print shell output in a file

J

Juergen Huber

hello,

one more question i will have!

now i have written a little programm, which delivers me an output on the
shell!


here is the print command, which delivers me the following output (see
below) on the shell:


print '%-30s | %-12d | %-12d |%-12d ' % (typename,
size / count,
count,
size)



--------------NAME-------------|Groesse(Byte)-|----Anzahl----|-Gesamtgroesse
(Byte)-|
----------------------------------------------------------------------------
----------
ADD_REAL_N6 | 4 | 1 |4
AND_BOOL_N10 | 4 | 1 |4
AND_BOOL_N12 | 4 | 1 |4



Is there a way to put this output in an file?!?! i searched about 2h for
this, but i couldn`t find an answer!

thnks, juergen
 
T

tac-tics

It sounds like you want to use print >>

If you have an open object with a "write" property, you can do
print >> thefile, mystring
and Python will simply redirect the output to "thefile" instead of
sys.stdout.
 
S

Stephan Wassipaul

f = file('output.txt','w')
print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
size / count,
count,
size)
f.close()
 
J

Juergen Huber

hello,

if i would type in your code, i became the following output in the
"output.txt" - file


BOOL | 4 | 50463 |201852


but why?!
he wouldn´t do that for all the entrys in the csv file! but only for the
first one in the file!
 
J

Juho Schultz

Juergen said:
hello,

one more question i will have!

now i have written a little programm, which delivers me an output on the
shell!
Is there a way to put this output in an file?!?! i searched about 2h for
this, but i couldn`t find an answer!

thnks, juergen

Others have suggested creating a file.

You could also let the shell to do the work.

If you run the program "normally", output goes to screen:
python myprog.py

Run and redirect the output to a file:
python myprog.py > output_of_myprog.txt

This also makes controlling the output filename much easier.

You could find the following useful:
http://www.swc.scipy.org/lec/shell01.html
 
S

Scott David Daniels

Juergen Huber wrote:
....
here is the print command, which delivers me the following output (see
below) on the shell:
print '%-30s | %-12d | %-12d |%-12d ' % (typename,
size / count,
count,
size) ....
Is there a way to put this output in an file?!?!

Another way nobody has yet mentioned:

import sys
old_output, sys.stdout = sys.stdout, open('somefile.txt', 'w')

try:
<<<put call to original code here>>>
finally:
old_output, sys.stdout = sys.stdout, old_output
old_output.close()
print 'Output safely written to:', old_output.name

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top