Skip to content

Instantly share code, notes, and snippets.

@konstantinfarrell
Created May 29, 2016 22:18
Show Gist options
  • Save konstantinfarrell/3cc966098f4a8b7b7702bc9111132107 to your computer and use it in GitHub Desktop.
Save konstantinfarrell/3cc966098f4a8b7b7702bc9111132107 to your computer and use it in GitHub Desktop.
Python script that cleans up my downloads folder
import shutil
import mimetypes
from os import listdir
from os.path import isfile, join
ORIGIN = r"C:\Users\Konstantin\Downloads\\" # Change these. Unless your system
# happens to have the same configuration.
MUSIC_DESTINATION = r"C:\Users\Konstantin\Music\\"
PICTURE_DESTINATION = r"C:\Users\Konstantin\Pictures\\"
PDF_DESTINATION = r"C:\Users\Konstantin\Documents\\"
ISO_DESTINATION = r"C:\Users\Konstantin\Documents\ISO\\"
ZIP_DESTINATION = r"C:\Users\Konstantin\Downloads\ZIPPED\\"
MSI_DESTINATION = r"C:\Users\Konstantin\Documents\MSI\\"
PPT_DESTINATION = r"C:\Users\Konstantin\Documents\\"
def move_files(origin, destination, filetype):
# Keep track of how many files get moved
total = 0
# Loop through the origin directory.
for file in listdir(origin):
filepath = join(origin, file)
if isfile(filepath):
# Uncomment if you want to see all the mimetypes of all the files.
#print(mimetypes.guess_type(filepath)[0], file, file.endswith(filetype))
try:
if filetype in mimetypes.guess_type(filepath)[0]:
try:
shutil.move(origin+file, destination)
total += 1
except shutil.Error as e:
pass
except TypeError as e:
# Cant figure out the mimetype, so we'll just guess based on the file extension.
if file.endswith(filetype):
try:
shutil.move(origin+file, destination)
total += 1
except shutil.Error as e:
pass
return total
def print_status(total, filetype):
# Print a nice little message
if total > 0:
print(total, filetype, "files were found and moved.")
else:
print("No", filetype, "files were found.")
print_status(move_files(ORIGIN, MUSIC_DESTINATION, 'audio'), 'audio')
print_status(move_files(ORIGIN, PICTURE_DESTINATION, 'image'), 'image')
print_status(move_files(ORIGIN, PDF_DESTINATION, 'pdf'), 'pdf')
print_status(move_files(ORIGIN, ZIP_DESTINATION, 'zip'), 'zip')
print_status(move_files(ORIGIN, ISO_DESTINATION, '.iso'), 'iso')
print_status(move_files(ORIGIN, MSI_DESTINATION, '.msi'), 'msi')
print_status(move_files(ORIGIN, PPT_DESTINATION, 'application/vnd.ms-powerpoint'), 'Power Point')
print_status(move_files(ORIGIN, PPT_DESTINATION, '.pptx'), 'Power Point')
print_status(move_files(ORIGIN, PPT_DESTINATION, 'application/msword'), 'Word')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment