PDA

View Full Version : [SOLVED] Making something print out to a txt file using python



blithen
March 13th, 2008, 07:29 PM
I know it's like..
sys.stdout
But that's all I know.
I have a python for dummies book but it just describes very briefly what stdout DOES not how to USE it.
Can anyone help me out?

days_of_ruin
March 13th, 2008, 07:37 PM
dont know but this probably should have been posted in the programming board.
I think "a byte of python" says something about that.
http://www.swaroopch.com/byteofpython/

blithen
March 13th, 2008, 07:40 PM
I was thinking about posting it in the programming board but isn't that mostly for programming to help the new release of Ubuntu?

bapoumba
March 13th, 2008, 07:50 PM
Moved to "Programming Talk".

pmasiar
March 13th, 2008, 08:03 PM
process a file line by line:


for line in file('file.name'):
parts = line.split('delim')
# process parts

is a start :-)

WW
March 13th, 2008, 08:10 PM
blithen, I don't understand your question. Could you say a little more about what you are trying to do?

Martin Witte
March 13th, 2008, 08:12 PM
if your file is a text file you could print it with


import sys
for line in file('.profile'):
sys.stdout.write(line)



if you use


for line in file('.profile'):
print line


you get interlining, as print adds a newline after each argument

dwblas
March 13th, 2008, 08:30 PM
Making something print out to a txt file using pythonIf you mean that you want to put something into a text file see the following. You want to look for "file write" not sys.stdout which sends the string to stdout=terminal usually.
fp=open("test.txt", "w") ## "w" means write to a new file / overwrite if file exists
fp.write("test line #1\n")
fp.write("test line #2\n")
fp.close()

## add to an existing file
fp=open("test.txt", "a") ## "a" means append/add to an existing file
fp.write("test line #3\n")
fp.write("test line #4\n")
fp.close()

blithen
March 14th, 2008, 04:33 AM
If you mean that you want to put something into a text file see the following. You want to look for "file write" not sys.stdout which sends the string to stdout=terminal usually.
fp=open("test.txt", "w") ## "w" means write to a new file / overwrite if file exists
fp.write("test line #1\n")
fp.write("test line #2\n")
fp.close()

## add to an existing file
fp=open("test.txt", "a") ## "a" means append/add to an existing file
fp.write("test line #3\n")
fp.write("test line #4\n")
fp.close()
Thank you!
It even has a page about this in my Python for Dummies book.:guitar: