Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import pathlib
- from pip._vendor.distlib.compat import raw_input
- def alphabitical(dir):
- # this method sorts the filenames in alphabitical order, displays the new order and renames the files based on user specifications.
- # array: file names without path
- array = sorted(os.listdir(dir))
- # arraydirs: file names with path (sorted)
- arraydirs = []
- # loops through all file names in the array
- for x in array:
- # if the directory doesnt end with "\". this statement will add "\" after the file path to insure that the path is correct
- if dir[len(dir) - 1] == chr(92):
- line = dir + x
- else:
- line = dir + '\\' + x
- # adds line to the array
- arraydirs.append(line)
- # prints the new order.
- print('---------------------')
- print('Sorting order:')
- print('---------------------')
- for x in arraydirs:
- print(x)
- print('---------------------')
- #correctInput: to indecate if the loop should be repeated or not
- correctInput = False
- while not correctInput:
- newFileName = raw_input("Enter the new file name: ")
- if not newFileName.isalpha():
- print('Error: Wrong input. only alphabets are allowed.')
- correctInput = False
- else:
- correctInput = True
- counter = 1
- for x in arraydirs:
- #renaming each file
- print('Renaming (' + x + ') ...')
- ext = pathlib.Path(x).suffix
- os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
- counter += 1
- def bySize(dir):
- # this method sorts the filenames in by size, displays the new order and renames the files based on user specifications.
- # array: file names without path
- array = os.listdir(dir)
- # arraydirs: file names with path (sorted)
- arraydirs = []
- for x in array:
- # if the directory doesnt end with "\". this statement will add "\" after the file path to insure that the path is correct
- if dir[len(dir) - 1] == chr(92):
- line = dir + x
- else:
- line = dir + '\\' + x
- arraydirs.append(line)
- #sorting format (by size)
- arraydirs.sort(key=lambda f: os.stat(f).st_size, reverse=True)
- # prints the new order.
- print('---------------------')
- print('Sorting order:')
- print('---------------------')
- for x in arraydirs:
- print(x)
- print('---------------------')
- # correctInput: to indecate if the loop should be repeated or not
- correctInput = False
- while not correctInput:
- newFileName = raw_input("Enter the new file name: ")
- if not newFileName.isalpha():
- print('Error: Wrong input. only alphabets are allowed.')
- correctInput = False
- else:
- correctInput = True
- counter = 1
- # renaming each file
- for x in arraydirs:
- print('Renaming (' + x + ') ...')
- ext = pathlib.Path(x).suffix
- os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
- counter += 1
- def byDate(dir):
- array = os.listdir(dir)
- arraydirs = []
- for x in array:
- if dir[len(dir) - 1] == chr(92):
- line = dir + x
- else:
- line = dir + '\\' + x
- arraydirs.append(line)
- arraydirs.sort(key=os.path.getmtime)
- print('---------------------')
- print('Sorting order:')
- print('---------------------')
- for x in arraydirs:
- print(x)
- print('---------------------')
- correctInput = False
- while not correctInput:
- newFileName = raw_input("Enter the new file name: ")
- if not newFileName.isalpha():
- print('Error: Wrong input. only alphabets are allowed.')
- correctInput = False
- else:
- correctInput = True
- counter = 1
- for x in arraydirs:
- print('Renaming (' + x + ') ...')
- ext = pathlib.Path(x).suffix
- os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
- counter += 1
- def fn(dir):
- file_list = os.listdir(dir)
- print('Imported:', file_list)
- if __name__ == '__main__':
- print(f'Please enter the directory of the files you want to rename: (e.g F:\myDocuments' + "\\)\n")
- isDirectory = False
- str_Dir = ""
- while not isDirectory:
- if not str_Dir == "":
- print('Error: Wrong input. Please enter a valid folder path.')
- folderDir = raw_input("Enter Directory: ")
- str_Dir = folderDir
- isDirectory = os.path.isdir(folderDir)
- if isDirectory:
- fn(folderDir)
- pass
- print(
- 'Select sorting method:-\n1-By alphabetical order \n2-By file size(larger to smaller)\n3-By date (oldest to newest)')
- correctInput = False
- while not correctInput:
- selectionInt = raw_input("Enter your choice(1 - 3): ")
- if not selectionInt.isdigit():
- print('Error: Wrong input. only digits are allowed.')
- correctInput = False
- else:
- correctInput = True
- if int(selectionInt) > 3 or int(selectionInt) < 1:
- print('Error: Wrong input. please write a number between 1 - 3.')
- correctInput = False
- else:
- correctInput = True
- if int(selectionInt) == 1:
- alphabitical(folderDir)
- print('Done! all files has been renamed in alphabitical order. exiting ..')
- correctInput = True
- elif int(selectionInt) == 2:
- bySize(folderDir)
- print('Done! all files has been renamed (based on biggest to smallest size). exiting ..')
- correctInput = True
- elif int(selectionInt) == 3:
- byDate(folderDir)
- print('Done! all files has been renamed (based on date modified oldest to newest). exiting ..')
- correctInput = True
Advertisement
Add Comment
Please, Sign In to add comment