how do i keep python from printing a newline when it exits theprogram?

X

xegfault

Here is a simple script:
#!/usr/bin/python

print "No newline, please.",
# End of script

When the program runs, python is still printing a newline when the
program exits. How does one keep python from doing so?

Thanks!
 
P

Peter Otten

xegfault said:
print "No newline, please.",
# End of script

When the program runs, python is still printing a newline when the
program exits. How does one keep python from doing so?

Use sys.stdout.write() instead of print or clear the softspace flag

sys.stdout.softspace = False

after the print statement. Google 'site:mail.python.org exit newline' for
some background.

Peter
 
A

Alexander Schmolck

xegfault said:
Here is a simple script:
#!/usr/bin/python

print "No newline, please.",
# End of script

When the program runs, python is still printing a newline when the
program exits. How does one keep python from doing so?

class softspaceLess(object):
""" ab d
"""
def __init__(self, file=sys.stdout):
self.__dict__['_file'] = file
file.softspace = False
def __getattr__(self,a):
if a == 'softspace': return False
return getattr(self._file,a)
def __setattr__(self,a,v):
if a=='softspace': return
setattr(self._file,a,v)
'as
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top