Writing Multiple files at a times

Denis McMahon denismfmcmahon at gmail.com
Tue Jul 1 01:22:10 CEST 2014


On Mon, 30 Jun 2014 12:23:08 -0700, subhabangalore wrote:

> Thank you for your kind suggestion. But I am not being able to sort out,
> "fp = open( "scraped/body{:0>5d}.htm".format( n ), "w" ) "
> please suggest.

look up the python manual for string.format() and open() functions.

The line indicated opens a file for write whose name is generated by the 
string.format() function by inserting the number N formatted to 5 digits 
with leading zeroes into the string "scraped/bodyN.htm"

It expects you to have a subdir called "scraped" below the dir you're 
executing the code in.

Also, this newsgroup is *NOT* a substitute for reading the manual for 
basic python functions and methods.

Finally, if you don't understand basic string and file handling in 
python, why on earth are you trying to write code that arguably needs a 
level of competence in both? Perhaps as your starter project you should 
try something simpler, print "hello world" is traditional.

To understand the string formatting, try:

print "hello {:0>5d} world".format( 5 )
print "hello {:0>5d} world".format( 50 )
print "hello {:0>5d} world".format( 500 )
print "hello {:0>5d} world".format( 5000 )
print "hello {:0>5d} world".format( 50000 )

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list