Advertisement
Guest User

Organize.pyw

a guest
Dec 17th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import shutil, os
  2.  
  3. #Returns a file's new name based on the number of files in the destination folder.
  4. #If there's no pictures, it's renamed name0, if there's 100, it's renamed name100.
  5. def renameFile(source, destination):
  6.     numberOfFiles = 0
  7.     for item in os.listdir(destination):
  8.         numberOfFiles += 1
  9.     newName = (os.path.splitext(source)[0]).split('%')[0] + str(numberOfFiles) + os.path.splitext(source)[1]
  10.     os.rename(source, newName)
  11.     return newName
  12.  
  13. #Moves file from one location to another, with a new name.
  14. def moveFile(source, destination):
  15.     filename = source
  16.     filename = renameFile(filename, destination)
  17.     shutil.move(filename, destination)
  18.  
  19. #Moves files in current directory into repsective folders.
  20. #Makes folders if they don't exist.
  21. #Doesn't move a file if another file in the target directory is the same size in bytes,
  22. #as a rudimentary form of preventing duplicate pictures.
  23. def organizePictures():
  24.     for item in os.listdir(os.getcwd()):
  25.         if not os.path.isdir(item) and not item.endswith('.py') and not item.endswith('.pyw'):
  26.             folderName = (os.path.splitext(item)[0]).translate(None, '%') + " Folder"
  27.             folderSizes = []
  28.             if not os.path.isdir(folderName):
  29.                 os.mkdir(folderName)
  30.             for picFile in os.listdir(folderName):
  31.                 folderSizes.append(os.stat(folderName + "\\" + picFile).st_size)
  32.             if os.stat(item).st_size not in folderSizes:
  33.                 moveFile(item, folderName)
  34.             else:
  35.                 os.remove(item)
  36.  
  37. organizePictures()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement