'subprocess.check_output' extra new line?

sbremal at hotmail.com sbremal at hotmail.com
Fri Jan 4 18:51:33 CET 2013

Very good point, you are absolutely right:

# cygpath C:\\ | od -c
0000000   /   c   y   g   d   r   i   v   e   /   c  \n

'bash' manual also confirms it:

   Command Substitution
       Command substitution allows the output of a command to replace  the  command  name.   There  are  two
       Bash  performs  the  expansion  by  executing command and replacing the command substitution with the
--->       standard output of the command, with any  trailing  newlines  deleted.   Embedded  newlines  are  not
       deleted,  but they may be removed during word splitting.  The command substitution $(cat file) can be
       replaced by the equivalent but faster $(< file).
       When the old-style backquote form of substitution is used,  backslash  retains  its  literal  meaning
       except  when  followed by $, `, or \.  The first backquote not preceded by a backslash terminates the
       command substitution.  When using the $(command) form, all characters between the parentheses make up
       the command; none are treated specially.
       Command  substitutions may be nested.  To nest when using the backquoted form, escape the inner back-
       quotes with backslashes.
       If the substitution appears within double quotes, word splitting and pathname expansion are not  per-
       formed on the results.


> Date: Sat, 5 Jan 2013 03:14:46 +1100
> Subject: Re: 'subprocess.check_output' extra new line?
> From: rosuav at gmail.com
> To: python-list at python.org
> 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.
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list 		 	   		  

More information about the Python-list mailing list