Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created November 26, 2018 14:46
Show Gist options
  • Save t3knoid/cf1f6ac96b246cc6e41516a7a957244f to your computer and use it in GitHub Desktop.
Save t3knoid/cf1f6ac96b246cc6e41516a7a957244f to your computer and use it in GitHub Desktop.
This Python script will walk a specified sourcedir and copies files it finds and copies them into a specified destination folder subfolder using the file's extension
from shutil import copyfile
import os
count = 0
destinationdir = ''
sourcedir = ''
for (dirname, dirs, files) in os.walk(sourcedir):
for file in files:
filename, file_extension = os.path.splitext(file)
file_extension = file_extension.strip(".")
extension_path = destinationdir + '\\' + file_extension
if (os.path.isdir(extension_path) == False):
try:
print ('Creating ' + extension_path)
os.mkdir(extension_path)
except:
print ('Failed to create ' + extension_path)
try:
print ('Copying ' + dirname + '\\' + file + ' to ' + extension_path + '\\' + filename)
copyfile(dirname + '\\' + file,extension_path + '\\' + file)
except IOError as e:
print('Unable to copy' + filename + ' to ' + extension_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment