How do I create a directory and then within that directory create a file for every day of the year so they will be named 0101, 0102 and so on. Also what do I need to do so I can put some basic html code in each file lets say for now I want the files to just have <html> <body> </html> in each file for the 365 days in the year. Is there a quick way in python to do this
writing to a file in python
Collapse
X
-
[CODE=python]mypath = 'c:\\year' #Your directory here
import os #import the operating system module
os.mkdir(mypath ) #make the directory
for day in range(1,366): #make a for loop for every day in the year, use 366 instead of 365 - 366 won't be included.
f = open(mypath + '\\' + str(day) + '.html','w') # open the file
f.write('<html> \n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
f.close() #close the file
print 'Done!' # We're done![/CODE]
I'm pretty sure this is what you want. Yay! I was able to contribute back to this community finally. :) -
Code:import time,os month,day=time.strftime("%m %d",time.localtime()).split() dir2create=os.path.join("/home/somedir2create") os.mkdir(dir2create) os.chdir(dir2create) astring = """ <html> <body> Blah Blah </html> """ open("%s%s"%(month,day),"a").write(astring)
Comment
-
Originally posted by William Manley[CODE=python]mypath = 'c:\\year' #Your directory here
import os #import the operating system module
os.mkdir(mypath ) #make the directory
for day in range(1,366): #make a for loop for every day in the year, use 366 instead of 365 - 366 won't be included.
f = open(mypath + '\\' + str(day) + '.html','w') # open the file
f.write('<html> \n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
f.close() #close the file
print 'Done!' # We're done![/CODE]
I'm pretty sure this is what you want. Yay! I was able to contribute back to this community finally. :)
This is awesome. Now, how would I change this so that the files would be named in month,day format like 0101 for January 1st.?Comment
-
Originally posted by grantstechThis is awesome. Now, how would I change this so that the files would be named in month,day format like 0101 for January 1st.?Code:months = range(1,13) days = [31,28,31,30,31,30,31,31,30,31,30,31] fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
... print i
...
0101.txt
0102.txt
0103.txt
0104.txt
0105.txt
0106.txt
............
0128.txt
0129.txt
0130.txt
0131.txt
0201.txt
0202.txt
0203.txt
............Comment
-
Thank you, thats a big help.
Originally posted by bvdetThis is one way to create a file list using month/date:Code:months = range(1,13) days = [31,28,31,30,31,30,31,31,30,31,30,31] fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
... print i
...
0101.txt
0102.txt
0103.txt
0104.txt
0105.txt
0106.txt
............
0128.txt
0129.txt
0130.txt
0131.txt
0201.txt
0202.txt
0203.txt
............Comment
-
Ok I was able to make a new directory then put the files in there for all 365 days in year 0101.txt, 0102, and so on now what do I need to do to tell it to open each file and write the same thing to each file, they are all going to be html files so I want to put just <html><body></body></html> for now so I can add to each later.
Thanks for all the helpComment
-
Originally posted by bvdetThis is one way to create a file list using month/date:Code:months = range(1,13) days = [31,28,31,30,31,30,31,31,30,31,30,31] fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
... print i
...
0101.txt
0102.txt
0103.txt
0104.txt
0105.txt
0106.txt
............
0128.txt
0129.txt
0130.txt
0131.txt
0201.txt
0202.txt
0203.txt
............
Thanks for all the helpComment
-
Originally posted by texas22Ok I was able to make a new directory then put the files in there for all 365 days in year 0101.txt, 0102, and so on now what do I need to do to tell it to open each file and write the same thing to each file, they are all going to be html files so I want to put just <html><body></body></html> for now so I can add to each later.
Thanks for all the helpCode:fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
f = open(fName, 'w')
f.write("Don't forget the/nnewline character for multiline text")
f.close()[/CODE]Comment
-
Originally posted by bartoncOnce you have a list of file names, as in:Code:fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
f = open(fName, 'w')
f.write("Don't forget the/nnewline character for multiline text")
f.close()[/CODE]
So I was able to get it to work but it made all those 300 something files and put them on my C/drive how do I tell it to put all the files in the directory files that I createdComment
-
writing to a file in python
Ok guys, I have asked about this in another discussion but here it goes if I have an html file for everyday of the year such as 0101 for January 1st 0102 and so on and I want to write to it so that each file will have <html><body>0 1/01(pertaining to the name of the file then it file 0102 it would have the date 01/02 so pretty much I'm what I need to do is for every file I need for it to have the date that pertains to that file so it should look something like
Python
(code)
f.write("<html> \n<body>\n01/02<hr>\n</body>\n</html&html")
but for the first file the 01/02 needs to be 01/01 to match the file name then file two which is 0102 needs to have the date 01/02 to match the name of that file name
Is this making any sense??Comment
-
Originally posted by texas22Ok guys, I have asked about this in another discussion but here it goes if I have an html file for everyday of the year such as 0101 for January 1st 0102 and so on and I want to write to it so that each file will have <html><body>0 1/01(pertaining to the name of the file then it file 0102 it would have the date 01/02 so pretty much I'm what I need to do is for every file I need for it to have the date that pertains to that file so it should look something like
Python
(code)
f.write("<html> \n<body>\n01/02<hr>\n</body>\n</html&html")
but for the first file the 01/02 needs to be 01/01 to match the file name then file two which is 0102 needs to have the date 01/02 to match the name of that file name
Is this making any sense??
[CODE=python]
>>> for i in range(1,13):
... fileName = r"thisisapath\0 1%02d" %i
... htmlLine = "<html>\n<body> \n01/%02d<hr>\n</body>\n</html&html" %i
... print fileName
... print repr(htmlLine)
... [/CODE]
thisisapath\010 1
'<html>\n<body> \n01/01<hr>\n</body>\n</html&html'
thisisapath\010 2
'<html>\n<body> \n01/02<hr>\n</body>\n</html&html'
thisisapath\010 3
'<html>\n<body> \n01/03<hr>\n</body>\n</html&html'
thisisapath\010 4
'<html>\n<body> \n01/04<hr>\n</body>\n</html&html'
thisisapath\010 5
'<html>\n<body> \n01/05<hr>\n</body>\n</html&html'
thisisapath\010 6
'<html>\n<body> \n01/06<hr>\n</body>\n</html&html'
thisisapath\010 7
'<html>\n<body> \n01/07<hr>\n</body>\n</html&html'
thisisapath\010 8
'<html>\n<body> \n01/08<hr>\n</body>\n</html&html'
thisisapath\010 9
'<html>\n<body> \n01/09<hr>\n</body>\n</html&html'
thisisapath\011 0
'<html>\n<body> \n01/10<hr>\n</body>\n</html&html'
thisisapath\011 1
'<html>\n<body> \n01/11<hr>\n</body>\n</html&html'
thisisapath\011 2
'<html>\n<body> \n01/12<hr>\n</body>\n</html&html'
>>>Comment
-
Originally posted by texas22Ok guys, I have asked about this in another discussion <snip>
For now, it's not a lot of trouble for me to do it, but you should learn the workings of the site sooner than later.
Thanks.Comment
-
Originally posted by ghostdog74Code:import time,os month,day=time.strftime("%m %d",time.localtime()).split() dir2create=os.path.join("/home/somedir2create") os.mkdir(dir2create) os.chdir(dir2create) astring = """ <html> <body> Blah Blah </html> """ open("%s%s"%(month,day),"a").write(astring)
Comment
Comment