473,385 Members | 1,279 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,385 software developers and data experts.

FileIO problem

i'm havin a problem with this simple program. i need the output of this
loop to be written to a file but i dont know how to do it!! this is
what i have right now:

gclas = raw_input("What is the class:")
count = 0
while count != 1000:
count = count + 1
print "Admin forceclass " , count , gclas

i know the IO script. i tried calling it a function, but i got an error
that says i cant call funtions for writing:

gclas = raw_input("What is the class:")
def Princlas():
count = 0
while count != 1000:
count = count + 1
print "Admin forceclass " , count , gclas
#asks for file name
a = raw_input("What is new file name:")
out_file = open(a,"w")
#this is the input of text
out_file.write(Princlas)
out_file.close()

i know i need to get it to a string but i dont know how?Any solution or
help i s more that welcome

Aug 24 '05 #1
4 1281
Layin'_Low enlightened us with:
count = 0
while count != 1000:
count = count + 1
print "Admin forceclass " , count , gclas


I think you want:

output = file('out', 'w')
gclas = raw_input("What is the class:")
for count in range(1000):
output.write("Admin forceclass %4i %s\n" % (count , gclas))
output.close()
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Aug 24 '05 #2
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 )
#asks for file name
a = raw_input("What is new file name:")
out_file = open(a,"w")
#this is the input of text
out_file.write(Princlas())
out_file.close()

i know i need to get it to a string but i dont know how?


Your functions should RETURN a string, with the "return" statement.
Another issue: you cannot write the function itself, only its result:

out_file.write(Princlas())

instead of

out_file.write(Princlas)
Aug 24 '05 #3
thnx guys it was late and had just come from a bar and didnt reread the
lines. thnx again for the help

Aug 24 '05 #4
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 'o****@xiludom.gro'.split('@')])"
Aug 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
1
by: Moe | last post by:
I have a winForm control that contains an openFileDialog. I am using this control in a webForm by enbedding it using an <object> tag. I need to access some information about the file on the client....
0
by: Steven Smith | last post by:
Hi guy's I've hit a bit of a sticking point, I'm sure this is simple enough but I just can't get my head around it and I'm looking for a bit of direction. When dealing with FileIO up until now I...
8
by: Vai2000 | last post by:
Hi All, How do I access a shared Drive which requires username and password to access? I need to write a file in a folder within it TIA
3
by: Marquis | last post by:
I am writing a ActiveX dll using Visual C# However , i am facing fileio and environment permission problem. Is there any way to solve this problem? just as signing the dll or other method to...
0
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi All, I have the following: FileIO.FileSystem.CopyDirectory(strFromString, strLocString, FileIO.UIOption.AllDialogs) Q: How do I access the UI that is presented and re-positon the x,y...
1
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
hi all, I have a button (stopcopy) that I want the user to cancel out of a long file copying operation. I'm not clear what I should "adding" in the sub to make this happen besides & return....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.