473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

writing to a file in python

26
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
Jun 12 '07 #1
16 2413
Expand|Select|Wrap|Line Numbers
  1. mypath = 'c:\\year' #Your directory here
  2. import os #import the operating system module
  3. os.mkdir(mypath) #make the directory
  4. 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.
  5.     f = open(mypath + '\\' + str(day) + '.html','w') # open the file
  6.     f.write('<html>\n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
  7.     f.close() #close the file
  8. print 'Done!' # We're done!
I'm pretty sure this is what you want. Yay! I was able to contribute back to this community finally. :)
Jun 13 '07 #2
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. import time,os
  2. month,day=time.strftime("%m %d",time.localtime()).split()
  3. dir2create=os.path.join("/home/somedir2create")
  4. os.mkdir(dir2create)
  5. os.chdir(dir2create)
  6. astring = """
  7. <html>
  8. <body>
  9. Blah Blah
  10. </html>
  11. """
  12. open("%s%s"%(month,day),"a").write(astring)
  13.  
Jun 13 '07 #3
Expand|Select|Wrap|Line Numbers
  1. mypath = 'c:\\year' #Your directory here
  2. import os #import the operating system module
  3. os.mkdir(mypath) #make the directory
  4. 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.
  5.     f = open(mypath + '\\' + str(day) + '.html','w') # open the file
  6.     f.write('<html>\n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
  7.     f.close() #close the file
  8. print 'Done!' # We're done!
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.?
Jun 14 '07 #4
bvdet
2,851 Expert Mod 2GB
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.?
This is one way to create a file list using month/date:
Expand|Select|Wrap|Line Numbers
  1. months = range(1,13)
  2. days = [31,28,31,30,31,30,31,31,30,31,30,31]
  3. fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
>>> for i in fileList:
... 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
............
Jun 14 '07 #5
Thank you, thats a big help.


This is one way to create a file list using month/date:
Expand|Select|Wrap|Line Numbers
  1. months = range(1,13)
  2. days = [31,28,31,30,31,30,31,31,30,31,30,31]
  3. fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
>>> for i in fileList:
... 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
............
Jun 15 '07 #6
texas22
26
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 help
Jun 15 '07 #7
texas22
26
This is one way to create a file list using month/date:
Expand|Select|Wrap|Line Numbers
  1. months = range(1,13)
  2. days = [31,28,31,30,31,30,31,31,30,31,30,31]
  3. fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
>>> for i in fileList:
... 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
............
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 help
Jun 15 '07 #8
bartonc
6,596 Expert 4TB
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 help
Once you have a list of file names, as in:
Expand|Select|Wrap|Line Numbers
  1. fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
you can iterate through that like this:
Expand|Select|Wrap|Line Numbers
  1. for fName in fileList:
  2.     f = open(fName, 'w')
  3.     f.write("Don't forget the/nnewline character for multiline text")
  4.     f.close()
Jun 15 '07 #9
texas22
26
Once you have a list of file names, as in:
Expand|Select|Wrap|Line Numbers
  1. fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
you can iterate through that like this:
Expand|Select|Wrap|Line Numbers
  1. for fName in fileList:
  2.     f = open(fName, 'w')
  3.     f.write("Don't forget the/nnewline character for multiline text")
  4.     f.close()
Awesome thanks,

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 created
Jun 15 '07 #10
texas22
26
Pretty sure I got it Thanks for all the help guys
Jun 15 '07 #11
texas22
26
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>01/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??
Jun 18 '07 #12
bartonc
6,596 Expert 4TB
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>01/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??
Something like this, perhaps?
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(1,13):
  2. ...     fileName = r"thisisapath\01%02d" %i
  3. ...     htmlLine = "<html>\n<body>\n01/%02d<hr>\n</body>\n</html&html" %i
  4. ...     print fileName
  5. ...     print repr(htmlLine)
  6. ...     
thisisapath\0101
'<html>\n<body>\n01/01<hr>\n</body>\n</html&html'
thisisapath\0102
'<html>\n<body>\n01/02<hr>\n</body>\n</html&html'
thisisapath\0103
'<html>\n<body>\n01/03<hr>\n</body>\n</html&html'
thisisapath\0104
'<html>\n<body>\n01/04<hr>\n</body>\n</html&html'
thisisapath\0105
'<html>\n<body>\n01/05<hr>\n</body>\n</html&html'
thisisapath\0106
'<html>\n<body>\n01/06<hr>\n</body>\n</html&html'
thisisapath\0107
'<html>\n<body>\n01/07<hr>\n</body>\n</html&html'
thisisapath\0108
'<html>\n<body>\n01/08<hr>\n</body>\n</html&html'
thisisapath\0109
'<html>\n<body>\n01/09<hr>\n</body>\n</html&html'
thisisapath\0110
'<html>\n<body>\n01/10<hr>\n</body>\n</html&html'
thisisapath\0111
'<html>\n<body>\n01/11<hr>\n</body>\n</html&html'
thisisapath\0112
'<html>\n<body>\n01/12<hr>\n</body>\n</html&html'
>>>
Jun 18 '07 #13
bartonc
6,596 Expert 4TB
Ok guys, I have asked about this in another discussion <snip>
If you need to know how to find your old posts, we can teach you.
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.
Jun 18 '07 #14
akbkgp
2
Expand|Select|Wrap|Line Numbers
  1. import time,os
  2. month,day=time.strftime("%m %d",time.localtime()).split()
  3. dir2create=os.path.join("/home/somedir2create")
  4. os.mkdir(dir2create)
  5. os.chdir(dir2create)
  6. astring = """
  7. <html>
  8. <body>
  9. Blah Blah
  10. </html>
  11. """
  12. open("%s%s"%(month,day),"a").write(astring)
  13.  
Thanks for the script, I almost succeed to write a similar m file for my python code. Now I'm having a problem, suppose I want to write the file with some specific no.s/characters previously defined in .py file. str() doesn't help me feeding the data to the m file to be written, what's the procedure ? Thanks.
Sep 27 '07 #15
bartonc
6,596 Expert 4TB
Thanks for the script, I almost succeed to write a similar m file for my python code. Now I'm having a problem, suppose I want to write the file with some specific no.s/characters previously defined in .py file. str() doesn't help me feeding the data to the m file to be written, what's the procedure ? Thanks.
Expand|Select|Wrap|Line Numbers
  1. import time,os
  2. month,day=time.strftime("%m %d",time.localtime()).split()
  3. dir2create=os.path.join("/home/somedir2create")
  4. os.mkdir(dir2create)
  5. os.chdir(dir2create)
  6. astring = """
  7. <html>
  8. <body>
  9. Blah Blah
  10. </html>
  11. """
  12. theFile = open("%s%s"%(month,day),"a")
  13. theFile.write(astring)
  14. theFile.close()
  15.  
Sep 27 '07 #16
akbkgp
2
thanks for the help, I haven't checked the place for long.
Mar 14 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Tad Marko | last post by:
Howdy! I'm trying to get my head around Python and I've come to realize that there are a lot of idioms in Python that are a bit different than in other languages. I have a class similar to what...
3
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead...
0
by: Bernard Lebel | last post by:
Hello, I have this strange problem. I have written a program that writes a log to a log file. To start the program (I'm on Linux Fedora Core 3), I used a service. This service runs a bash...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
42
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.