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

Discussion in 'Python' started by S. Staats, Oct 11, 2004.

  1. S. Staats

    S. Staats Guest

    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!
     
    S. Staats, Oct 11, 2004
    #1
    1. Advertising

  2. 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?


    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
     
    Alex Martelli, Oct 11, 2004
    #2
    1. Advertising

  3. 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')
     
    Mahesh Padmanabhan, Oct 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,949
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    66,041
    sandeep1976
    Nov 8, 2006
  3. xegfault
    Replies:
    2
    Views:
    335
    Alexander Schmolck
    Oct 11, 2004
  4. Harlin Seritt

    Sys.exit() does not fully exit

    Harlin Seritt, Feb 17, 2008, in forum: Python
    Replies:
    3
    Views:
    295
    Christian Heimes
    Feb 17, 2008
  5. hisan
    Replies:
    1
    Views:
    1,384
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page