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

Discussion in 'Python' started by xegfault, Oct 11, 2004.

  1. xegfault

    xegfault Guest

    Here is a simple script:

    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?

    xegfault, Oct 11, 2004
    1. Advertisements

  2. xegfault

    Peter Otten Guest

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

    sys.stdout.softspace = False

    after the print statement. Google ' exit newline' for
    some background.

    Peter Otten, Oct 11, 2004
    1. Advertisements

  3. 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
    Alexander Schmolck, Oct 11, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.