Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > print shell output in a file

Reply
Thread Tools

print shell output in a file

 
 
Juergen Huber
Guest
Posts: n/a
 
      06-30-2006
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

 
Reply With Quote
 
 
 
 
tac-tics
Guest
Posts: n/a
 
      06-30-2006
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.

 
Reply With Quote
 
 
 
 
Stephan Wassipaul
Guest
Posts: n/a
 
      06-30-2006
f = file('output.txt','w')
print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
size / count,
count,
size)
f.close()
> 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
>

 
Reply With Quote
 
Juergen Huber
Guest
Posts: n/a
 
      06-30-2006
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:
> f = file('output.txt','w')
> print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
> size / count,
> count,
> size)
> f.close()
>> 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

sse
>> (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


 
Reply With Quote
 
Juho Schultz
Guest
Posts: n/a
 
      06-30-2006

Juergen Huber wrote:
> 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

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      07-01-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.popen output different from native shell output nickname Python 7 08-26-2009 02:18 PM
Re: Problem - I want to print Current Output of Pdf file and shouldprint once.I get print dialog box but it is not working Lew Java 0 05-30-2007 01:31 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Wscript.Shell run PRINT and NET PRINT returns nothing Salty Dog ASP General 4 02-28-2005 06:45 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57