Generate a list of file names based on month and year arithmetic
Posted
by
MacUsers
on Stack Overflow
See other posts from Stack Overflow
or by MacUsers
Published on 2011-02-14T04:19:32Z
Indexed on
2011/02/14
23:25 UTC
Read the original article
Hit count: 279
python
How can I list the numbers 01
to 12
(one for each of the 12 months) in such a way so that the current month always comes last where the oldest one is first. In other words, if the number is grater than the current month, it's from the previous year.
e.g. 02
is Feb, 2011 (the current month right now), 03
is March, 2010 and 09
is Sep, 2010 but 01
is Jan, 2011. In this case, I'd like to have [09, 03, 01, 02]
. This is what I'm doing to determine the year:
for inFile in os.listdir('.'):
if inFile.isdigit():
month = months[int(inFile)]
if int(inFile) <= int(strftime("%m")):
year = strftime("%Y")
else:
year = int(strftime("%Y"))-1
mnYear = month + ", " + str(year)
I don't have a clue what to do next. What should I do here?
Update:
I think, I better upload the entire script for better understanding.
#!/usr/bin/env python
import os, sys
from time import strftime
from calendar import month_abbr
vGroup = {}
vo = "group_lhcb"
SI00_fig = float(2.478)
months = tuple(month_abbr)
print "\n%-12s\t%10s\t%8s\t%10s" % ('VOs','CPU-time','CPU-time','kSI2K-hrs')
print "%-12s\t%10s\t%8s\t%10s" % ('','(in Sec)','(in Hrs)','(*2.478)')
print "=" * 58
for inFile in os.listdir('.'):
if inFile.isdigit():
readFile = open(inFile, 'r')
lines = readFile.readlines()
readFile.close()
month = months[int(inFile)]
if int(inFile) <= int(strftime("%m")):
year = strftime("%Y")
else:
year = int(strftime("%Y"))-1
mnYear = month + ", " + str(year)
for line in lines[2:]:
if line.find(vo)==0:
g, i = line.split()
s = vGroup.get(g, 0)
vGroup[g] = s + int(i)
sumHrs = ((vGroup[g]/60)/60)
sumSi2k = sumHrs*SI00_fig
print "%-12s\t%10s\t%8s\t%10.2f" % (mnYear,vGroup[g],sumHrs,sumSi2k)
del vGroup[g]
When I run the script, I get this:
[root@serv07 usage]# ./test.py
VOs CPU-time CPU-time kSI2K-hrs
(in Sec) (in Hrs) (*2.478)
==================================================
Jan, 2011 211201372 58667 145376.83
Dec, 2010 5064337 1406 3484.07
Feb, 2011 17506049 4862 12048.04
Sep, 2010 210874275 58576 145151.33
As I said in the original post, I like the result to be in this order instead:
Sep, 2010 210874275 58576 145151.33
Dec, 2010 5064337 1406 3484.07
Jan, 2011 211201372 58667 145376.83
Feb, 2011 17506049 4862 12048.04
The files in the source directory reads like this:
[root@serv07 usage]# ls -l
total 3632
-rw-r--r-- 1 root root 1144972 Feb 9 19:23 01
-rw-r--r-- 1 root root 556630 Feb 13 09:11 02
-rw-r--r-- 1 root root 443782 Feb 11 17:23 02.bak
-rw-r--r-- 1 root root 1144556 Feb 14 09:30 09
-rw-r--r-- 1 root root 370822 Feb 9 19:24 12
Did I give a better picture now? Sorry for not being very clear in the first place. Cheers!!
Update @Mark Ransom
This is the result from Mark's suggestion:
[root@serv07 usage]# ./test.py
VOs CPU-time CPU-time kSI2K-hrs
(in Sec) (in Hrs) (*2.478)
==========================================================
Dec, 2010 5064337 1406 3484.07
Sep, 2010 210874275 58576 145151.33
Feb, 2011 17506049 4862 12048.04
Jan, 2011 211201372 58667 145376.83
As I said before, I'm looking for the result to b printed in this order: Sep, 2010 -> Dec, 2010 -> Jan, 2011 -> Feb, 2011 Cheers!!
© Stack Overflow or respective owner