are final newlines appended if output lacks it?

Tim Peters tim.one at comcast.net
Sat May 3 23:59:14 EDT 2003


[Dan Jacobson]
> Doing help() print explains how the blank got in here,
>
> $ python -c "print 'wow',;print 'pow',"|od -c
> 0000000   w   o   w       p   o   w  \n
> 0000010
>
> But not the newline. Why?

[John Machin]
> derrrrr ..... my brain was in neutral. Dan's question/problem is quite
> valid, not explained by the documentation, and I have been able to
> reproduce it on Windows 2000 with Python 2.2.2.
>
> Doing this give the same effect as what Dan got, except that of course
> with Windows the offending extra is '\r\n' instead of just '\n':
>
> python -c "print 'wow',;print 'pow'," >powwow.txt
>
> Doing this:
>
> python -c "import sys;sys.stdout.write('abc');sys.stdout.write('xyz')"
>           > abcxyz.txt
>
> produces no newline.  Looks like there's an undocumented "feature" of
> the Python's print statement, nothing to do with the C stdio library
> or the OS.

OTOH, you'll find that

    python -c "print 'abc\n',"

produces 4 (on Linux) or 5 (on Windows) characters.  It has to do with
whether sys.stdout.softspace is set when the program is done.
PyRun_SimpleFileExFlags() ends with

	if (Py_FlushLine())
		PyErr_Clear();
	return 0;

and Py_FlushLine() is

int
Py_FlushLine(void)
{
	PyObject *f = PySys_GetObject("stdout");
	if (f == NULL)
		return 0;
	if (!PyFile_SoftSpace(f, 0))
		return 0;
	return PyFile_WriteString("\n", f);
}

So it's specifically sys.stdout (and only sys.stdout) that may get a newline
tacked on, and the relation to print is that normally only print fiddles
with a file's softspace attribute.

Code would break if we changed this, so learn to love it <wink>.  I expect
that in Python 3, there won't be any softspace gimmick.






More information about the Python-list mailing list