Tuesday, May 18, 2010

Python!

So someone asked me a little while ago how to get some set of files to be sorted on creation date and then have a number in the filename to indicate the sorting. I figured it'd be easy with bash, but being the bash newbie I am, I switched to do some python. Using some quick googling, I wrote the code down there.
It does things pretty simple: it uses os.stat to query the ctime, puts it in a tuple together with the filename, adds it all to a list, sorts that list and then copies the files to their new names.

import os, glob, sys, shutil
from stat import ST_CTIME
path = '.'

lijstje = []
for f in glob.glob( os.path.join(path, '*') ):
lijstje.append((os.stat(f)[ST_CTIME],f))

counter = 0
lijstje = sorted(lijstje)
for x in lijstje:
date, name = x
if counter <10:
dest = "00%d%s"%(counter,name[2:])
elif counter <100:
dest = "0%d%s"%(counter,name[2:])
else:
dest = "%d%s"%(counter,name[2:])
#print dest
shutil.copy(name, dest)
counter +=1


Here's what it does:


namnatulco@namnatulco-desktop:~/test/subdir$ ls -Al
total 4
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:45 testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:45 test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 x


I like to see what happens, so I commented the print line in the code out. Here's what it outputs:

namnatulco@namnatulco-desktop:~/test/subdir$ python test.py
000a
001x
002b
003f
004hello_thar
005test
006testfile
007test.py

And the resulting ls -Al:

namnatulco@namnatulco-desktop:~/test/subdir$ ls -Al
total 8
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 000a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 001x
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 002b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 003f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 004hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 005test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 006testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:46 007test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:45 testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:45 test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 x


edit: yes, I know this is probably a lot easier in just plain bash, but I just like to play around in python. Besides, this'll probably work on windows :)

No comments:

Post a Comment