Skip to content

Instantly share code, notes, and snippets.

@jfrantz1-r7
Created March 6, 2019 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfrantz1-r7/10e5372aab910989f5b5c64245101dee to your computer and use it in GitHub Desktop.
Save jfrantz1-r7/10e5372aab910989f5b5c64245101dee to your computer and use it in GitHub Desktop.
import os
import shutil
fullpath = os.path.join
python_directory = "./py"
start_directory = "./mixed"
text_files = "./txt"
default_path = "./default"
#This is the dictionary to look up file extensions
dir_lookup = {
'docx': './docx/',
'txt': './txt/'
}
def get_extension(filename):
return filename[filename.rfind('.')+1:]
def main():
for dirname, dirnames, filenames in os.walk(start_directory):
for filename in filenames:
source = fullpath(dirname, filename)
extension = get_extension(filename)
if (extension in dir_lookup):
shutil.move(source, fullpath(dir_lookup[extension], filename))
else:
shutil.move(source, fullpath(default_path, filename))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment