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

Alexander Schmolck a.schmolck at gmx.net
Mon Oct 11 13:06:56 EDT 2004


"xegfault" <sstaats at questia.com> writes:

> 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):
    """
    >>> import sys
    >>> print "a", "b", " ", "d"
    a b   d
    >>> print >>softspaceLess(sys.stdout), "a", "b", " ", "d"
    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



More information about the Python-list mailing list