Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created October 17, 2014 01:40
Show Gist options
  • Save gregtemp/fda8da8128a8446cba27 to your computer and use it in GitHub Desktop.
Save gregtemp/fda8da8128a8446cba27 to your computer and use it in GitHub Desktop.
Organizes your files into folders named after file types.
#
# Organizes your files into folders named after file types
# "Why?" you ask? Hard to say.
#
# Please note that all changes are permanent, and duplicates will be overwritten.
# All in all, not a super useful tool. I actually don't recommend using it.
# Also, I am not liable if you screw all your folders/files up with this.
import os
import errno
import sys
rootdir = ''
type_dict={}
print("\n## Hello and Welcome to Organizer! ##\n")
print("Organizer organizes your files into folders that are named after the file types.")
print("For example, a file named 'greatFile.txt' would be put into a folder named 'txt'.")
print("One might ask why this would be useful...")
print("The journey through life leaves many unanswered questions...")
print("\n Please note that all changes are permanent,\n so don't screw this up. \n")
def organize_rootdir():
if sys.version_info[0] < 3:
rootdir = raw_input("Please enter the complete path to the\ndirectory you'd like to organize: ")
else:
rootdir = input("Please enter the complete path to the\ndirectory you'd like to organize: ")
print(rootdir)
while True:
if rootdir[-1] != "/":
rootdir += "/"
if os.path.exists(rootdir):
break
else:
if sys.version_info[0] < 3:
rootdir = raw_input("\n...Lets try that again.\n Please enter the complete path to the\n directory you'd like to organize: ")
else:
rootdir = input("\n...Lets try that again.\n Please enter the complete path to the\n directory you'd like to organize: ")
for file in os.listdir(rootdir):
type = ''
# Ignore hidden files
if file[0] == ".":
continue
# Get the file type (characters after the .)
for index, char in enumerate(reversed(file)):
if char == '.':
if type not in type_dict:
type_dict[type] = [file]
else:
type_dict[type].append(file)
break
type = char + type
type = type.lower()
# Dictionary with file types as keys
print(type_dict)
if len(type_dict) == 0:
print("There were no loose files to organize.")
# For each file type, make a directory
for key in type_dict:
# Check if directory exists, if not, make directory
try:
os.makedirs(rootdir + key)
print("Making directory named '%s'" %(key))
# Copy files into new directory
for i in range(0, len(type_dict[key])):
print(" Copying '%s' into '%s'" %(type_dict[key][i], key))
os.rename(rootdir + type_dict[key][i], rootdir + key + "/" + type_dict[key][i])
except OSError as exception:
# If exception is anything except (directory already exists)
if exception.errno != errno.EEXIST:
raise
else:
print ("Directory named '%s' already exists." %(key) )
for i in range(0, len(type_dict[key])):
print(" Copying '%s' into '%s'" %(type_dict[key][i], key))
os.rename(rootdir + type_dict[key][i], rootdir + key + "/" + type_dict[key][i])
print("\nThank you for using Organizer. It has been a pleasure organizing your files.")
concluding_thoughts()
def concluding_thoughts():
# Say thank you and goodbye (check if user wants to do it again)
if sys.version_info[0] < 3:
again = raw_input("Would you like to organize another folder? (y/n): ")
else:
again = input("Would you like to organize another folder? (y/n): ")
if again == "y":
organize_rootdir()
elif again == "n":
print("Fine. Whatever.")
sys.exit(0)
else:
print("Please enter y for yes, or n for no. There are no other options.\nTry again.\n\n")
concluding_thoughts()
organize_rootdir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment