Welcome, guest | Sign In | My Account | Store | Cart

This program make some file named year+month+day How to use "python program argument example:argument=01,02,...,12 If you input 02,it will return files named 20120201,...,20120228

Python, 43 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# coding: utf-8
import sys
from datetime import datetime
#easier way by Massimiliano
#import sys
#from datetime import date
#month=sys.argv[1]
#my_date=date(date.today().year,month,1)
#while my_date.month==month:
#    filename=my_date.strftime('%Y%m%d')
#    f=open(filename,'w')
#    f.write('#'+filename)
#    f.close()
#    my_date=date.fromordinal(my_date.toordinal()+1)
#
def day_month(m):
    """
    change date at the end of month
    """
    if(s=="02"):
        return 28
    elif(m=="04" or m=="06" or m=="09" or m=="11"):
        return 30
    else:
        return 31
d=datetime.now()
year=str(d.year)

numberday=day_month(sys.argv[1])
for x in range(1,numberday+1):

    if(x<10):
        num = str(x).zfill(2) #before "0"+str(x)

    else:
        num=str(x)

    filename= year + sys.argv[1] + num
    
    f = open(filename,'w')
    f.write("#"+filename)
    f.close()

6 comments

ryotaro goto (author) 12 years, 1 month ago  # | flag

I'm beginner programing!! If you think that here is such a good way,please advise me plus I'm a beginner to speak English(^_^)

Patrycja Szabłowska 12 years, 1 month ago  # | flag

You can use str(x).zfill(2) instead of "0"+str(x).

ryotaro goto (author) 12 years, 1 month ago  # | flag

Thank you!! That way is easy to read code My code is edited

Massimiliano 12 years, 1 month ago  # | flag

Maybe an easier way could be:

from datetime import date month = sys.argv[1] my_date = date(date.today().year, month, 1)

while my_date.month == month: filename = my_date.strftime('%Y%m%d') print filename # write your file
my_date = date.fromordinal( my_date.toordinal() + 1 )

Massimiliano 12 years, 1 month ago  # | flag
from datetime import date
month = sys.argv[1] 
my_date = date(date.today().year, month, 1)
while my_date.month == month:
    filename = my_date.strftime('%Y%m%d')
    print filename
    # write your file    
    my_date = date.fromordinal( my_date.toordinal() + 1 )
ryotaro goto (author) 12 years, 1 month ago  # | flag

it's cool!!(^o^)

I don't know date objects.

When i checked docs,i miss thing!!

i understand this way.thank you!!