Skip to main content
2 of 4
deleted 26 characters in body; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Moving and sorting files in download folder

This is a Python script that I use on my Android tablet. It moves the files in the download folder to a folder on the external SD card. The files are sorted based on their extensions and are moved to folders (for each different type of extension). If it encounters a new file type, it will create a new folder. If the file name already exists, the new file will be renamed by appending the first five hash digits to the end of the file name. The original file is deleted.

How can I improve my program? What changes should I make?

import os
import hashlib

download_dir = "/sdcard/Download/"
save_dir = "/mnt/external_sd/Download/"

FileList = os.listdir(download_dir)
for File in FileList:
    #print File
    extension = ''.join(os.path.splitext(File)[1])
    name = ''.join(os.path.splitext(File)[0])
    ext = extension.strip('.')
    
    if os.path.isdir(download_dir + File):
        pass
    else:
        if os.path.exists(save_dir + ext + "/" + File):
            Data = open(download_dir + File, "r").read()
            m = hashlib.sha1() 
            m.update(Data) 
            h = (m.hexdigest())[0:5]
            file(save_dir + ext + "/" +name+"-"+h+"."+ext, "w").write(Data)
            print File, " ","-->"," ",name+"-"+h+"."+ext
            os.remove(download_dir + File)
        
        elif os.path.exists(save_dir + ext):
            Data = open(download_dir + File, "r").read()
            file(save_dir + ext + "/" + File, "w").write(Data)
            print File #, " ","-->"," ",name+"."+ext
            os.remove(download_dir + File)

        if os.path.exists(save_dir + ext) != True:
            os.makedirs(save_dir + ext)
            Data = open(download_dir + File, "r").read()
            file(save_dir + ext + "/" + File, "w").write(Data)
            print File , " ","-->"," ","/"+ext+"/"+name+"."+ext
            os.remove(download_dir + File)



        
kyle k
  • 1.5k
  • 3
  • 15
  • 22