Dad, Family Alpha, Software engineer, Heroes of the Storm and Minecraft player, Anime otaku, Metal and Goa enthusiast. https://www.visualcv.com/viktor-vad
Saturday, 23 July 2011
Python: move files to creation date named directories
Some years earlier I needed to have a little script to sort out tons of photos imported from my camera. The criteria was to put every photo into it's own directory by creation date. First I made this script in 2.6 but lately updated to 3.x.
I got it working on my Win7 box with these changes:
import os, time, shutil, sys
dir = 'c:/temp' os.chdir(dir) for f in os.listdir('.'): ftime = time.gmtime(os.path.getmtime(f)) ctime_dir = str(ftime.tm_year) + '-' + str(ftime.tm_mon) + '-' + str(ftime.tm_mday) if not os.path.isdir(ctime_dir): os.mkdir(ctime_dir) print(ctime_dir) dst = ctime_dir + '/' + f shutil.move(f, dst) print('File' + f + 'has been moved to' + dst)
Thank you for your addition! You could also go to the Gist on Github and create a Pull Request with your modifications, alternatively comment there with syntax highlight. Thanks :)
I cannot get this to work.
ReplyDeleteBe more specific please! Saying things like "I's not working" won't help anybody to help in solving your problem with it.
DeleteI got it working on my Win7 box with these changes:
ReplyDeleteimport os, time, shutil, sys
dir = 'c:/temp'
os.chdir(dir)
for f in os.listdir('.'):
ftime = time.gmtime(os.path.getmtime(f))
ctime_dir = str(ftime.tm_year) + '-' + str(ftime.tm_mon) + '-' + str(ftime.tm_mday)
if not os.path.isdir(ctime_dir):
os.mkdir(ctime_dir)
print(ctime_dir)
dst = ctime_dir + '/' + f
shutil.move(f, dst)
print('File' + f + 'has been moved to' + dst)
Thank you for your addition! You could also go to the Gist on Github and create a Pull Request with your modifications, alternatively comment there with syntax highlight. Thanks :)
Delete