[Tutor] How to write string in a file, each one on new line???
Folks, I dont like to swamp your mailboxes with my trivial questions. But please tell me why this isnt working?? str = str + '\n' fd.write(str) # fd is teh file handle the traceback is as under: Traceback (most recent call last): File scrap.py, line 39, in ? fd.write(str + '\n')IOError: (0, 'Error') -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write string in a file, each one on new line???
Asrarahmed Kadri wrote: Folks, I dont like to swamp your mailboxes with my trivial questions. We don't mind trivial questions, this is a list for beginners. But there is no need to swamp our mailboxes by asking the same question more than once, and if you include complete code and complete error tracebacks you have a much better chance of getting a good answer. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write this to a file?
print outputs the string to the std output (sys.stdout). You could redirect the std output to a file instead of the screen as follows, but it's a bit clumsy. fptr = open(hans.log, a+) import sys sys.stdout = fptr print Hi there This will output the string into the file hans.log. However, to get it to print to the screen again, u will need to have stored the original contents of sys.stdout somewhere and then redirected sys.stdout to that again. TCL provides a more handy way of doing this: print $fptr Hi there Cheers Hans -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dick Moores Sent: Thursday, 6 October 2005 12:18 p.m. To: tutor@python.org Subject: [Tutor] How to write this to a file? I have a script that writes it's output to a file. I also print the time with print Time was %.4g seconds % (timeEnd - timeStart) How could I also have the same output of the print expression, written to the file? Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write this to a file?
Dick Moores wrote: I have a script that writes it's output to a file. I also print the time with print Time was %.4g seconds % (timeEnd - timeStart) How could I also have the same output of the print expression, written to the file? The formatting part of the print is just an expression with a string value, you can assign it to a variable and write it to your file. You might want to add a newline: timeMsg = Time was %.4g seconds\n % (timeEnd - timeStart) f.write(timeMsg) If you have a lot of output that you want to put to the console and to a log file you might like to look at the logging module. A single line of logging can be written to multiple places by the module. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor