FileIO problem

bruno modulix onurb at xiludom.gro
Wed Aug 24 13:57:35 EDT 2005


Laszlo Zsolt Nagy wrote:
> Try this:
> 
> gclas = raw_input("What is the class:")
> def Princlas():
>    count = 0
>    while count != 1000:
>        count = count + 1
>        return "Admin forceclass %s %s " % ( count , gclas )

have you tried your code ? Obviously, no, else you would have seen that
it always return on the first iteration.

<op>
2 solutions : passing the output stream to the function, or having the
function returning a string.

# solution 1
def prin_clas(gclas, out, nblines=1000):
   for count in xrange(nblines):
	out.write("Admin forceclass %d %s\n" % ( count , gclas ))


a = raw_input("What is new file name:")
out_file = open(a,"w")
gclas = raw_input("What is the class:")
prin_clas(gclas, out_file)
out_file.close()

# solution 2
def prin_clas(gclas, nb_lines=1000):
   result = []
   for count in xrange(nblines):
	result.append("Admin forceclass %d %s" % ( count , gclas ))
   return "\n".join(result)

a = raw_input("What is new file name:")
out_file = open(a,"w")
gclas = raw_input("What is the class:")
out_file.write(prin_clas(gclas))
out_file.close()




-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list