n0s3c

main.py

Feb 12th, 2021
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. import os
  2. import pathlib
  3. from pip._vendor.distlib.compat import raw_input
  4.  
  5.  
  6. def alphabitical(dir):
  7.     # this method sorts the filenames in alphabitical order, displays the new order and renames the files based on user specifications.
  8.     # array: file names without path
  9.     array = sorted(os.listdir(dir))
  10.     # arraydirs: file names with path (sorted)
  11.     arraydirs = []
  12.     # loops through all file names in the array
  13.     for x in array:
  14.         # if the directory doesnt end with "\". this statement will add "\" after the file path to insure that the path is correct
  15.         if dir[len(dir) - 1] == chr(92):
  16.             line = dir + x
  17.         else:
  18.             line = dir + '\\' + x
  19.         # adds line to the array
  20.         arraydirs.append(line)
  21.     # prints the new order.
  22.     print('---------------------')
  23.     print('Sorting order:')
  24.     print('---------------------')
  25.     for x in arraydirs:
  26.         print(x)
  27.     print('---------------------')
  28.     #correctInput: to indecate if the loop should be repeated or not
  29.     correctInput = False
  30.     while not correctInput:
  31.         newFileName = raw_input("Enter the new file name: ")
  32.         if not newFileName.isalpha():
  33.             print('Error: Wrong input. only alphabets are allowed.')
  34.             correctInput = False
  35.  
  36.         else:
  37.             correctInput = True
  38.             counter = 1
  39.  
  40.             for x in arraydirs:
  41.                 #renaming each file
  42.                 print('Renaming (' + x + ') ...')
  43.                 ext = pathlib.Path(x).suffix
  44.                 os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
  45.                 counter += 1
  46.  
  47.  
  48. def bySize(dir):
  49.     # this method sorts the filenames in by size, displays the new order and renames the files based on user specifications.
  50.     # array: file names without path
  51.     array = os.listdir(dir)
  52.     # arraydirs: file names with path (sorted)
  53.     arraydirs = []
  54.  
  55.     for x in array:
  56.         # if the directory doesnt end with "\". this statement will add "\" after the file path to insure that the path is correct
  57.         if dir[len(dir) - 1] == chr(92):
  58.             line = dir + x
  59.         else:
  60.             line = dir + '\\' + x
  61.         arraydirs.append(line)
  62.     #sorting format (by size)
  63.     arraydirs.sort(key=lambda f: os.stat(f).st_size, reverse=True)
  64.     # prints the new order.
  65.     print('---------------------')
  66.     print('Sorting order:')
  67.     print('---------------------')
  68.     for x in arraydirs:
  69.         print(x)
  70.     print('---------------------')
  71.     # correctInput: to indecate if the loop should be repeated or not
  72.     correctInput = False
  73.     while not correctInput:
  74.         newFileName = raw_input("Enter the new file name: ")
  75.         if not newFileName.isalpha():
  76.             print('Error: Wrong input. only alphabets are allowed.')
  77.             correctInput = False
  78.  
  79.         else:
  80.             correctInput = True
  81.             counter = 1
  82.             # renaming each file
  83.             for x in arraydirs:
  84.                 print('Renaming (' + x + ') ...')
  85.                 ext = pathlib.Path(x).suffix
  86.                 os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
  87.                 counter += 1
  88.  
  89.  
  90. def byDate(dir):
  91.     array = os.listdir(dir)
  92.     arraydirs = []
  93.     for x in array:
  94.         if dir[len(dir) - 1] == chr(92):
  95.             line = dir + x
  96.         else:
  97.             line = dir + '\\' + x
  98.         arraydirs.append(line)
  99.     arraydirs.sort(key=os.path.getmtime)
  100.     print('---------------------')
  101.     print('Sorting order:')
  102.     print('---------------------')
  103.     for x in arraydirs:
  104.         print(x)
  105.     print('---------------------')
  106.     correctInput = False
  107.     while not correctInput:
  108.         newFileName = raw_input("Enter the new file name: ")
  109.         if not newFileName.isalpha():
  110.             print('Error: Wrong input. only alphabets are allowed.')
  111.             correctInput = False
  112.         else:
  113.             correctInput = True
  114.             counter = 1
  115.             for x in arraydirs:
  116.                 print('Renaming (' + x + ') ...')
  117.                 ext = pathlib.Path(x).suffix
  118.                 os.rename(x, dir + '\\' + newFileName + ' ' + str(counter) + ext)
  119.                 counter += 1
  120.  
  121.  
  122. def fn(dir):
  123.     file_list = os.listdir(dir)
  124.     print('Imported:', file_list)
  125.  
  126.  
  127. if __name__ == '__main__':
  128.  
  129.     print(f'Please enter the directory of the files you want to rename: (e.g F:\myDocuments' + "\\)\n")
  130.     isDirectory = False
  131.     str_Dir = ""
  132.     while not isDirectory:
  133.         if not str_Dir == "":
  134.             print('Error: Wrong input. Please enter a valid folder path.')
  135.         folderDir = raw_input("Enter Directory: ")
  136.         str_Dir = folderDir
  137.         isDirectory = os.path.isdir(folderDir)
  138.         if isDirectory:
  139.             fn(folderDir)
  140.             pass
  141.             print(
  142.                 'Select sorting method:-\n1-By alphabetical order \n2-By file size(larger to smaller)\n3-By date (oldest to newest)')
  143. correctInput = False
  144. while not correctInput:
  145.     selectionInt = raw_input("Enter your choice(1 - 3): ")
  146.     if not selectionInt.isdigit():
  147.         print('Error: Wrong input. only digits are allowed.')
  148.         correctInput = False
  149.     else:
  150.         correctInput = True
  151.         if int(selectionInt) > 3 or int(selectionInt) < 1:
  152.             print('Error: Wrong input. please write a number between 1 - 3.')
  153.             correctInput = False
  154.         else:
  155.             correctInput = True
  156.             if int(selectionInt) == 1:
  157.                 alphabitical(folderDir)
  158.                 print('Done! all files has been renamed in alphabitical order. exiting ..')
  159.                 correctInput = True
  160.  
  161.             elif int(selectionInt) == 2:
  162.  
  163.                 bySize(folderDir)
  164.                 print('Done! all files has been renamed (based on biggest to smallest size). exiting ..')
  165.                 correctInput = True
  166.             elif int(selectionInt) == 3:
  167.  
  168.                 byDate(folderDir)
  169.                 print('Done! all files has been renamed (based on date modified oldest to newest). exiting ..')
  170.                 correctInput = True
  171.  
Advertisement
Add Comment
Please, Sign In to add comment