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

S

S. Staats

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!
 
A

Alex Martelli

S. Staats said:
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
 
M

Mahesh Padmanabhan

S. Staats said:
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')
 

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,053
Latest member
BrodieSola

Latest Threads

Top