1

In my code I am trying to print without a new line after the program exits and print again. I am printing with a comma after for example:

type_d=dict.fromkeys(totals,[0,0])
for keysl in type_d.keys():
    print type_d[keysl][0] , ",", type_d[keysl][1] , ",",
print "HI" ,

But when the program exits and I call on another one there is a newline inputted after the last value in the file. How can I avoid this?

user6656104
  • 51
  • 1
  • 10
  • 1
    Can you provide more complete code, e.g. a [MCVE]? This shouldn't create any newlines as written, unless it turns out values in `x` have embedded newlines. – ShadowRanger Jul 04 '18 at 00:27
  • Well I even added print "Hi", after the loop and it was only a newline after Hi, basically always the last value before the program exits – user6656104 Jul 04 '18 at 00:30
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Braca Jul 04 '18 at 00:40
  • Are you sure this is being done by Python, not by whatever editor you're using to view the file? You might try viewing the raw file hex (prior to opening in any editor) with `xxd` or the like (choose your favorite ultra-barebones tool for displaying hex) to make sure the newline is really there, not added "for convenience" by the viewer you're using. – ShadowRanger Jul 04 '18 at 00:40
  • @Braca: The OP is already doing the correct thing to avoid a newline, and is fine with the space, so not really a dupe. – ShadowRanger Jul 04 '18 at 00:41
  • @ShadowRanger but the accepted answer is the same – Braca Jul 04 '18 at 00:45
  • @Braca: Not anymore; this new answer from abarnert was quite enlightening. Just because the problem can be solved the same way by coincidence doesn't mean it's a duplicate. – ShadowRanger Jul 04 '18 at 00:53
  • @ShadowRanger Yes, great answer – Braca Jul 04 '18 at 00:55
  • @ShadowRanger In your comment about an editor… are you assuming the OP is doing `python hi.py >out.txt` to capture it, or (since I think this was before there was an MCVE in the question) that the code had `print >>outfile "Hi",` in it? Either way, the easier way to see it is just `python hi.py; python hi.py` and see if the two strings end up on one line. – abarnert Jul 04 '18 at 01:03
  • @abarnert: Yeah, I was assuming piping to a file at command line. Your approach to checking works quite nicely, I just happen to love hex display for showing you the exact file contents when it's non-printing or semi-visible characters you're concerned with. – ShadowRanger Jul 04 '18 at 01:05
  • Fun thing for someone to try (I probably won’t be back in front of a computer for a few hours): if you manually close stdout before exit, what happens? My guess is that you don’t get the newline, but I wouldn’t be too surprised if I’m wrong. – abarnert Jul 04 '18 at 01:09
  • @ShadowRanger Yeah, it’s definitely a useful tool to put in the OP’s toolbox for, say, the next time he runs into one of those fun Python 2 encoding messes where you can’t tell whether the terminal is the problem… although I usually tell people to just open rb and print the repr, assuming they’ll be more familiar with slightly obscure Python than even common Unix tools. – abarnert Jul 04 '18 at 01:13

2 Answers2

3

I believe that this is not documented, but it's intentional, and related to behavior that is documented.

If you look up print in the docs:

A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except ' ', or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.)

And Python does keep track of whether the last thing written to sys.stdout was written by print (including by print >>sys.stdout) or not (unless sys.stdout is not an actual file object). (See PyFile_SoftSpace in the C API docs.) That's how it does (3) above.

And it's also what it uses to decide whether to print a newline when closing stdout.

So, if you don't want that newline at the end, you can just do the same workaround mentioned in (3) at the end of your program:

for i in range(10):
    print i,
print 'Hi',
sys.stdout.write('')

Now, when you run it:

$ python hi.py && python hi.py
0 1 2 3 4 5 6 7 8 9 Hi0 1 2 3 4 5 6 7 8 9 Hi

If you want to see the source responsible:

The PRINT_ITEM bytecode handler in ceval is where the "soft space" flag gets set.

The code that checks and outputs a newline or not depending on the flag is Py_FlushLine (which is also used by many other parts of Python).

The code that calls that check is, I believe, handle_system_exit—but notice that the various different "Very High Level C API" functions for running Python code in the same file also do the same thing at the end.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    Man, this just reminds me why every Python module I write that needs Py2 compatibility begins with `from __future__ import print_function`. None of this special purpose statement nonsense with weird side-effects. Learned something new today, and it mostly reminded me why I avoided learning that particular dusty corner of the old language. :-) – ShadowRanger Jul 04 '18 at 00:51
  • @ShadowRanger Yeah, the `print` statement is very weird, and soft-space is one of the weirdest parts of it. It works like magic and DWIM 90% of the time, and it works like magic and I can't understand how to make it do what I _actually_ mean the other 10% of the time… – abarnert Jul 04 '18 at 00:57
0

You can try to use the code below, it will eliminate the new line:

import sys
sys.stdout.write("HI")