user3582082
user3582082

Reputation: 31

Trying to create files for each day in a month

I'm trying to create one file for each day of the year, so 31 for january, 28 for february and so on, but since it's a school work i cant have a to long code either, so a smart way to do this would be nice. At them moment i'm trying this (below) but it says i cant use a list as a range object

def MonthList():

    lenghtOnMonthList = [32,29,32,31,32,31,32,32,31,32,31,32]

    return lenghtOnMonthList


def CreateFile(lenghtOnMonthList):

    for month in range(1,13):

        if month < 10:

            month = "0" + str(month)

        day = 1 

        for day in range(1,lenghtOnMonthList):

            if day < 10:

                day = "0" + str(day)

            file = open(str(month) + str(day)+'.txt', "a")

            day = int(day)

            day += 1

basicly i wanna name each file 0101 for january the first, 0102 for the second and all the way to 1231 and ocfourse skip 0229 (since i use 28 days in february)

but why cant i use my list to show that for month one do 32 days (since it gives 31) and for month 2 do 29 days?

Thanks ahead //Kasper

Upvotes: 2

Views: 440

Answers (1)

Shrey
Shrey

Reputation: 1260

A small code that might solve your problem

from calendar import monthrange
import os
for i in range(1,13):
    x=monthrange(2014,i)
    for j in range(1,x[1]+1):
        cmd="%02d%02d" % (i,j)
        os.system("touch " + str(cmd))

touch command is used in unix based systems to create files. In case you are using windows system then you can use subprocess module of python. Please take a look at https://docs.python.org/2/library/subprocess.html if you want to use subprocess module

Upvotes: 5

Related Questions