'subprocess.check_output' extra new line?

Chris Angelico rosuav at gmail.com
Fri Jan 4 17:14:46 CET 2013

On Sat, Jan 5, 2013 at 2:50 AM,  <sbremal at hotmail.com> wrote:
> Hi
> I wonder if the additional new line charachter at the end of the standard output capture is on purpose with 'subprocess.check_output'?
>>>> subprocess.check_output([ 'cygpath', 'C:\\' ])
> '/cygdrive/c\n'
> If I do the same from the shell there is no extra new line (which is correct I believe):
> $ x=$(cygpath C:\\); echo "_${x}_"
> _/cygdrive/c_
> Surely I have a workaround. I was more interested whether it was a design flaw.

What you may have there is the shell $( ) handling changing the
program's output. Try piping the command into 'hd' or similar to see
what it actually produces; it's entirely possible the \n is there, and
the shell is stripping it.

In any case, you can easily trim whitespace from inside Python. That
would be your workaround, I think.


More information about the Python-list mailing list