Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Module Imports #
- import os # imports the OS module
- # Directory Variables #
- start_dir = "RELEVANT DIR HERE" # The directory in which I save my files to.
- final_dir = ["Pictures\\","eBooks\\","WordDocs\\","PowerPoints\\","Applications\\","Torrents\\","Archives\\","ISO\\"] # List of the folders I have created for the relevant file types
- # File Variable list #
- files_typ = [".jpg",".jpeg",".gif",".png",".bmp",".pdf",".ePub",".rtf",".doc",".docx",".txt",".ppt",".pptx",".exe",".msi",".torrent",".zip",".rar",".tar.gz",".iso"] # List of common file types I have downloaded.
- # Directory Code Code #
- os.chdir(start_dir) # Changes to the relevant downloads directory that way it does not have to be executed from the same directory.
- dir_list = os.listdir("./") # Makes a list of all the current files in the directory.
- new_list = [] # New list for the list clean function to go to.
- # List clean Function #
- def list_clean(): # Because my downloads folder contains subdirs this code will clean them out of the list
- Y = 0 # Y variable
- for x in dir_list: # Counts the amount of files (folders and files) in the directory
- if os.path.isdir(dir_list[Y]) == True: # if it's a directory, removes it from the list
- dir_list.remove(dir_list[Y])
- list_clean() # repeats its self until there are no more directories
- elif os.path.isfile(dir_list[Y]) == True: # if it's a file, adds it to a new list
- new_list.append(dir_list[Y]) # adds the file to a new list
- dir_list.remove(dir_list[Y]) # removes it from the old list completely
- list_clean() # loops until there are no more files in the list
- def file_move(): # Now it will move the files into the relative sub directories
- new_list.remove("desktop.ini") # desktop.ini is always included in folders, so removing it here is good practice.
- W = len(new_list) # Checks how many files are in the list
- X = 0
- Y = 0
- Z = 0
- while W > X: # A nested loop with a nested if statement.
- if len(new_list) >= 1: # if statement
- while Z < len(files_typ): # while statement
- if new_list[0].endswith(files_typ[Y]) == True:
- if new_list[0].endswith(files_typ[0]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[1]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[2]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[3]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[4]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[5]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[1]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[6]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[1]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[7]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[8]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[9]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[10]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[11]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[3]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[12]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[3]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[13]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[4]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[14]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[4]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[15]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[5]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[16]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[17]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[18]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[19]) == True:
- os.rename(start_dir+new_list[0],start_dir+final_dir[7]+new_list[0])
- new_list.remove(new_list[0])
- elif new_list[0].endswith(files_typ[Y]) != True:
- print files_typ[Y],"does not match",new_list[0]
- Y = Y + 1
- else:
- os.system("cls")
- print "No more files!"
- X = X + 1
- Z = Z + 1
- os.system("cls")
- print "\n No file matches, quitting now!"
- Z = Z + 1
- print "\n No files to sort!"
- os.system("cls")
- list_clean()
- file_move()
Add Comment
Please, Sign In to add comment