Connecting Tech Pros Worldwide Help | Site Map

print shell output in a file

Juergen Huber
Guest
 
Posts: n/a
#1: Jun 30 '06
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

tac-tics
Guest
 
Posts: n/a
#2: Jun 30 '06

re: print shell output in a file


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.

Stephan Wassipaul
Guest
 
Posts: n/a
#3: Jun 30 '06

re: print shell output in a file


f = file('output.txt','w')
print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
size / count,
count,
size)
f.close()[color=blue]
> 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
>[/color]
Juergen Huber
Guest
 
Posts: n/a
#4: Jun 30 '06

re: print shell output in a file


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!




Stephan Wassipaul wrote:[color=blue]
> f = file('output.txt','w')
> print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
> size / count,
> count,
> size)
> f.close()[color=green]
>> 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----|-Gesamtgroe[/color][/color]
sse[color=blue][color=green]
>> (Byte)-|
>> -------------------------------------------------------------------------[/color][/color]
---[color=blue][color=green]
>> ----------
>> 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[/color][/color]

Juho Schultz
Guest
 
Posts: n/a
#5: Jun 30 '06

re: print shell output in a file



Juergen Huber wrote:[color=blue]
> hello,
>
> one more question i will have!
>
> now i have written a little programm, which delivers me an output on the
> shell!
>[/color]
[color=blue]
>
> 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[/color]

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

Scott David Daniels
Guest
 
Posts: n/a
#6: Jul 1 '06

re: print shell output in a file


Juergen Huber wrote:
....[color=blue]
> 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)[/color]
....[color=blue]
> Is there a way to put this output in an file?!?![/color]

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
scott.daniels@acm.org
Closed Thread