Skip to content

Instantly share code, notes, and snippets.

@rakeshjuyal
Created June 17, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakeshjuyal/61a7adba5ec09cbe62a3b63a1e108390 to your computer and use it in GitHub Desktop.
Save rakeshjuyal/61a7adba5ec09cbe62a3b63a1e108390 to your computer and use it in GitHub Desktop.
Copy/Arrange files from source directory on the basis of creation date ( Year , Month )
'''
My first python program. So no exepection of optimized code ;)
What did I learn:
1. We can't concatenate String with integer/boolean... Use '{} {}'.format(var1, var2);
2. os Module have almost everything about Files and Directory.
3. os.walk returns files in subDirectory also. os.listDir returns file in currentDirectory only ( which we need ). os.scanDir can be faster in many cases
https://docs.python.org/3/library/os.html
https://docs.python.org/3/library/os.html#os.listdir
https://docs.python.org/3/library/os.html#os.scandir
https://docs.python.org/3/library/os.html#os.DirEntry
4. os.path.getctime // Not what I expected.
IDE:
VS Code
Extension: Python
v0.1:
Hello world
v0.2
list files in directory
v0.3
Get file creation time ( winows only ). Get Month and Year
Create directories in targetPath if not exist. Use mtime instead of ctime. I should use exif.
v0.4
Copy the file to appropriate directory.
v0.5
Support command line. Old school way. No argparse module
Optimization Needed:
1. Try to copy multiple files in same directory in one go. Not sure how to do it though.
2. Find fast way to get Month and Year of file.
'''
import sys;
import os;
import time;
import datetime;
import shutil;
#print ("First program");
path = '.';
#path = 'D:/backup/Pics/PythonTest'
#targetPath = 'D:/backup/Pics/PythonTestTarget'
if ( len(sys.argv) != 3 ):
print ('This needs exactly 2 inputs in commandline. Source and Destination.')
print ('py filesbydate.py "source-dir" "destination-dir".')
exit()
path = sys.argv[1];
targetPath = sys.argv[2];
allFiles = [];
i = 0;
#for ( dirPath, dirNames, fileNames ) in os.walk(path):
for ( file ) in os.scandir(path):
#print( '>> FileName {} : isFile: {}\n'.format(file, os.path.isfile( path + '/' + file)))
#fullPath = path + '/' + file
#fullPath = os.path.abspath(file); // DirEntry can give fullPath
#print( '>> FileName {} : isFile: {} : directIsFile: {} \n'.format(fullPath, os.path.isfile( fullPath), file.is_file() ));
#if ( os.path.isfile(fullPath)):
if( file.is_file()):
#allFiles.append(fullPath);
#allFiles.append(file.path);
#fileCTime = os.path.getctime(file); #Windows only? This is not correct.
fileCTime = os.path.getmtime(file); #Modified file time. But good for images!. May be I should try exif.
fileCTimeDTF = datetime.datetime.fromtimestamp(fileCTime)
fileCreationYear = fileCTimeDTF.strftime("%Y");
fileCreationMonth = fileCTimeDTF.strftime("%B");
targetPathToCopy = os.path.join(targetPath, fileCreationYear, fileCreationMonth);
os.makedirs(targetPathToCopy, exist_ok=True); # Create subdirectory if not exists
i = i + 1;
#print ( '>> {} {} {} {} {} {} {}\n'.
# format(i, file.path, fileCTime, fileCTimeDTF, fileCreationYear, fileCreationMonth, targetPathToCopy ) );
print('{} Copying {} to {}\n'.format( i, file.path, targetPathToCopy ) );
#shutil.move(file.path, targetPathToCopy ); #Finally move the file to correct folder.
shutil.copy(file.path, targetPathToCopy ); #Finally move the file to correct folder.
@rakeshjuyal
Copy link
Author

rakeshjuyal commented Jun 17, 2019

Use:
py filesbydate.py "source-dir" "destination-dir

I created this because my Android image folder had around 2000 image files. My Windows laptop was taking ages everytime I open this directory. Pretty sure there must be some apps which can do the same thing. But I never wrote any Python program so thought it will be good learning.

This is highly unoptimized code :D ~ Everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment