Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import shutil, os
- #Returns a file's new name based on the number of files in the destination folder.
- #If there's no pictures, it's renamed name0, if there's 100, it's renamed name100.
- def renameFile(source, destination):
- numberOfFiles = 0
- for item in os.listdir(destination):
- numberOfFiles += 1
- newName = (os.path.splitext(source)[0]).split('%')[0] + str(numberOfFiles) + os.path.splitext(source)[1]
- os.rename(source, newName)
- return newName
- #Moves file from one location to another, with a new name.
- def moveFile(source, destination):
- filename = source
- filename = renameFile(filename, destination)
- shutil.move(filename, destination)
- #Moves files in current directory into repsective folders.
- #Makes folders if they don't exist.
- #Doesn't move a file if another file in the target directory is the same size in bytes,
- #as a rudimentary form of preventing duplicate pictures.
- def organizePictures():
- for item in os.listdir(os.getcwd()):
- if not os.path.isdir(item) and not item.endswith('.py') and not item.endswith('.pyw'):
- folderName = (os.path.splitext(item)[0]).translate(None, '%') + " Folder"
- folderSizes = []
- if not os.path.isdir(folderName):
- os.mkdir(folderName)
- for picFile in os.listdir(folderName):
- folderSizes.append(os.stat(folderName + "\\" + picFile).st_size)
- if os.stat(item).st_size not in folderSizes:
- moveFile(item, folderName)
- else:
- os.remove(item)
- organizePictures()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement