Working version that supports python3 and handles moving files if the name collides with an existing file

This commit is contained in:
Chris Magnuson 2015-06-04 19:29:17 -04:00
parent a1885d37a3
commit 6e78b184ee
1 changed files with 11 additions and 8 deletions

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python2
import os
import os.path
import shutil
import sys
import time
source = sys.argv[1]
destination = sys.argv[2]
@ -15,11 +16,13 @@ while not os.path.exists(destination):
for root, dirs, files in os.walk(source, topdown=False):
for file in files:
extension = os.path.splitext(file)[1][1:].upper()
destinationPath = os.path.join(destination,extension)
if not os.path.exists(destinationPath):
destinationPath = os.path.join(destination,extension)
if not os.path.exists(destinationPath):
os.mkdir(destinationPath)
if os.path.exists(os.path.join(destinationPath,file)):
print 'WARNING: this file was not copied :' + os.path.join(root,file)
else:
shutil.copy2(os.path.join(root,file), destinationPath)
if os.path.exists(os.path.join(destinationPath,file)):
print("WARNING: this file was not copied :" + os.path.join(root,file))
shutil.move(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
else:
shutil.move(os.path.join(root,file), destinationPath)