From 6e78b184eec9dbce2630987d3196232fa1b230af Mon Sep 17 00:00:00 2001 From: Chris Magnuson Date: Thu, 4 Jun 2015 19:29:17 -0400 Subject: [PATCH] Working version that supports python3 and handles moving files if the name collides with an existing file --- recovery.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/recovery.py b/recovery.py index 7e480e1..583db98 100644 --- a/recovery.py +++ b/recovery.py @@ -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)