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:

cmd = "gpsmon -n 10.3.1.254:2947 -l output.txt"
gpsmonitor_cmd = subprocess.Popen(cmd.split(),stdout = subprocess.DEVNULL)

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:

process = subprocess.Popen(createBinaryCommand,stdout=subprocess.DEVNULL)   

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

Duration = 180.0 [sec]
                                                                    01   29.2   1.0  27629867.8   5.0
                                                                                                     02  279.0  22.1  29485316.6   3.1
                                                                                                                                      03   16.2  16.5  29757080.4   3.5
                                                                                                                                                                       04   58.0  48.8  30795329.9   1.9
                  05  222.1  32.8  28015547.1   2.5
                                                   06  307.9  21.2  30581471.1   3.2
                                                                                    07  209.8  81.7  31316191.8   1.5
                                                                                                                     08   75.1  43.6  29817969.2   2.1
                                                                                                                                                      09   59.0  75.3  32009836.6   1.5

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

cmd = "gpsmon -n 10.3.1.254:2947 -l output.txt"
gpsmonitor_cmd = subprocess.Popen(cmd.split(),stdout = subprocess.DEVNULL)

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

Duration = 180.0 [sec]
01   29.2   1.0  27629867.8   5.0
02  279.0  22.1  29485316.6   3.1
03   16.2  16.5  29757080.4   3.5
04   58.0  48.8  30795329.9   1.9
05  222.1  32.8  28015547.1   2.5
06  307.9  21.2  30581471.1   3.2
07  209.8  81.7  31316191.8   1.5
08   75.1  43.6  29817969.2   2.1
09   59.0  75.3  32009836.6   1.5
13  247.2  19.4  27856171.3   3.3
14  336.7  38.8  32242259.8   2.2
16  138.9  38.3  27297398.5   2.3
17  349.8   8.4  30063728.5   4.2
18  173.1  12.2  24256737.2   3.9
20  249.7  50.1  30679658.2   1.9
21   46.7   1.5  27617657.0   4.9

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:

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