Skip to content
Advertisement

Python’s subprocess.Popen() causes strange indentation of output

I have some lines of code that gather NMEA sentences from the gpsmon gpsd command line utility (spawned as a child process) and gathers some internal GPS location data from an external radio. I don’t want the user to see the gpsmon output in the terminal when they run the code, so I redirect it /dev/null like so:

JavaScript

Following this code, I execute a C binary executable from an external file, this as a subprocess as well, as well with it’s output redirected to dev null. I’m confused though, as this binary’s output still appears in the terminal. The subprocess call in question:

JavaScript

When I run the code, the output of the binary command looks like this:

JavaScript

Very oddly indented. And when I run the code without the code snippet:

JavaScript

The output of the C binary command now looks normal again:

JavaScript

I’m assuming this has something to do with the back to back child processes being spawned and their output redirecting to dev null, but I cannot figure out how to make the output look normally formatted again.

Advertisement

Answer

That indentation means you have only newlines without carriage returns, and you’re rendering into a terminal that’s configured to act in a Windows-y way.

On UNIXy systems, the linefeed character also sends the cursor to the left. On Windowsy systems, it only moves the cursor down, and you need a carriage return to send the cursor to the left.


To tell a UNIX terminal to treat a linefeed as if it were both a carriage return and a linefeed, you can use:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement