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.


4 comments:

  1. I cannot get this to work.

    ReplyDelete
    Replies
    1. Be more specific please! Saying things like "I's not working" won't help anybody to help in solving your problem with it.

      Delete
  2. 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)

    ReplyDelete
    Replies
    1. 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