Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How does one keep python from printing newline at program exit?

Reply
Thread Tools

How does one keep python from printing newline at program exit?

 
 
S. Staats
Guest
Posts: n/a
 
      10-11-2004
Good day, everbody.

Here is a simple program called test.py:
#!/usr/bin/python
print "No newline, please.",
# End of program

Here is what program does:
prompt> ./test.py
No newline, please.

Here is what I want the program to do:
prompt> ./test.py
No newline, please.prompt>

How can I get the python interpreter to not print a newline when it exits?

Thanks for your help!
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-11-2004
S. Staats <(E-Mail Removed)> wrote:

> Good day, everbody.
>
> Here is a simple program called test.py:
> #!/usr/bin/python
> print "No newline, please.",
> # End of program
>
> Here is what program does:
> prompt> ./test.py
> No newline, please.
>
> Here is what I want the program to do:
> prompt> ./test.py
> No newline, please.prompt>
>
> How can I get the python interpreter to not print a newline when it exits?


You can avoid using print -- use sys.output.write instead -- or you can
use sys.output.softspace=False as the last statement in your program.

The reason for the newline is that, when standard output (sys.output) is
closed, it checks if its softspace attribute is True (meaning there is a
pending line not yet terminated) and if so terminates the line. In
turn, softspace is set by print with a trailing comma, specifically to
avoid erroneously-unterminated lines (and other small anomalies). So,
if you want something a bit out of the ordinary such as an unterminated
line being output, either you eschew print, which is meant to help in
typical ordinary cases (sys.stdout.write being there for when you need
fine control), or you reset that special attribute to False.


Alex
 
Reply With Quote
 
 
 
 
Mahesh Padmanabhan
Guest
Posts: n/a
 
      10-11-2004
S. Staats wrote:
> Good day, everbody.
>
> Here is a simple program called test.py:
> #!/usr/bin/python
> print "No newline, please.",
> # End of program
>
> Here is what program does:
> prompt> ./test.py
> No newline, please.
>
> Here is what I want the program to do:
> prompt> ./test.py
> No newline, please.prompt>
>
> How can I get the python interpreter to not print a newline when it exits?
>
> Thanks for your help!


sys.stdout.write('No newline, please')
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How keep python socket alive for ever by setting Keep alive flag. hisan Python 1 06-25-2012 05:30 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
how do i keep python from printing a newline when it exits theprogram? xegfault Python 2 10-11-2004 05:06 PM
printing something without a newline OR a space after it? Alex Hunsley Python 4 06-03-2004 02:12 PM



Advertisments