Creating a list of file names based on arithmetic of the month and year

How can I list the numbers 01before 12(one for each of the 12 months) so that the current month is always the last, where the oldest of them is the first. In other words, if the number is greater than this month, then this was last year.

eg. 02- February 2011 (the current month right now), 03- March, 2010 and 09- September 2010, but 01- January 2011. In this case, I would like to have [09, 03, 01, 02]. This is what I do 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 have no idea what to do next. What should I do here?


Update:

I think I better downloaded the entire script for a 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 the following:

[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

, , :

Sep, 2010        210874275     58576     145151.33
Dec, 2010          5064337      1406       3484.07
Jan, 2011        211201372     58667     145376.83
Feb, 2011         17506049      4862      12048.04

:

[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

? , . !!


@Mark Ransom

:

[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

, , : Sep, 2010 → Dec, 2010 → , 2011 → 2011 !!

+3
3

, ! , - , , . , .

filelist = []
for inFile in os.listdir('.'):
    if inFile.isdigit():
        if int(inFile) <= int(strftime("%m")):
            year = strftime("%Y")
        else:
            year = int(strftime("%Y"))-1
        filelist.append((year, inFile))
filelist.sort()
for year, inFile in filelist:
    month = months[int(inFile)]
    ...
+1

:

import os, sys
from time import strftime
from calendar import month_abbr

thismonth = int(strftime("%m"))
sortedmonths = ["%02d" % (m % 12 + 1) for m in range(thismonth, thismonth + 12)]
inFiles = [m for m in sortedmonths if m in os.listdir('.')]

for inFile in inFiles:
    readFile = open(inFile, 'r')
    # [ ... everything else is the same from here (but reindented)... ]

, -, , 2.3, :

inFiles = filter(lambda x: x.isdigit(), os.listdir('.'))
sortedmonths = map(lambda x: "%02d" % (x % 12 + 1), range(thismonth, thismonth + 12))
inFiles = filter(lambda x: x in inFiles, sortedmonths)

:

, , :

import os, sys
from time import strftime
from calendar import month_abbr

thismonth = int(strftime("%m"))
inFiles = []
for inFile in os.listdir('.'):
    if inFile.isdigit():
        inFiles.append(inFile)
inFiles.sort(key=lambda x: (int(x) - thismonth - 1) % 12)

for inFile in inFiles:
    readFile = open(inFile, 'r')
    # [ ... everything else is the same from here (but reindented)... ]

, .

:

inFiles = [inFile for inFile in os.listdir('.') if inFile.isdigit()]

, from datetime import datetime, datetime.now(). , strftime() - datetime.now().month ( .year, .min, .second .., str(datetime.now()) .

Update:

, , , - , Jan = 1 Jan = 0:

>>> themonths = [1, 2, 3, 9]
>>> themonths.sort(key=lambda x: (x - thismonth - 1) % 12)
>>> themonths
[2, 3, 9, 1]

:

:

>>> thismonth = 1
>>> [m % 12 for m in range(thismonth + 1, thismonth + 13)]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1]

, , :

>>> thismonth = 1
>>> themonths = [0, 1, 2, 8]
>>> [m % 12 for m in range(thismonth + 1, thismonth + 13) if m % 12 in themonths]
[2, 8, 0, 1]

, Jan = 0 .... Dec = 11. Jan = 1, 1 (.. [m % 12 + 1 for ... if m % 12 + 1 in themonths]). , Jan = 0 - , .

+1

.

12 , , :

>>> def rot_list(l,n):
...    return l[n:]+l[:n]
... 
>>> rot_list(range(1,13),2)
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]

, , ( ), , ( n) , :

>>> tar=[1,2,3,9]
>>> n=2
>>> [x for x in range(1,13)[n:]+range(1,13)[:n] if x in tar]
[3, 9, 1, 2]
>>> tar=[3,1,2,9]
>>> [x for x in range(1,13)[n:]+range(1,13)[:n] if x in tar]
[3, 9, 1, 2]

, , , - :

>>> nm=[3,9,1,2]
>>> fn=['%02i' % x for x in nm]
>>> fn
['03', '09', '01', '02']

All of these methods work whether Jan is "0" or "1". Just change any link range(1,13)to range(0,12)depending on the agreement.

Edit

If I understand what you are doing, this code will help:

import datetime

def prev_month(year,month,day):
    l=range(1,13)
    l=l[month:]+l[:month]
    p_month=l[10]
    if p_month>month:
        p_year=year-1
    else:
        p_year=year
    return (p_year,p_month,1)

def next_month(year,month,day):
    l=range(1,13)
    l=l[month:]+l[:month]
    p_month=l[0]
    if p_month<month:
        p_year=year+1
    else:
        p_year=year
    return (p_year,p_month,1)

year=2011
month=2
t=(year-1,month,1)
ds=[]
for i in range(1,13):
    print datetime.date(*t).strftime('%m, %y')
    t=next_month(*t)

Conclusion:

02, 10
03, 10
04, 10
05, 10
06, 10
07, 10
08, 10
09, 10
10, 10
11, 10
12, 10
01, 11

Just put the appropriate file name format in the method strftime()...

Hope this is what you are looking for ...

0
source

Source: https://habr.com/ru/post/1791919/


All Articles