Problem working with subprocess.check_call
Peter Otten
__peter__ at web.de
Thu Oct 29 06:11:17 EDT 2015
David Aldrich wrote:
> I am working on Linux with Python 3.4.
>
> I want to do a bash diff on two text files and show just the first 20
> lines of diff's output. So I tried:
>
>>>> cmd = 'head -20 <(diff ' + file1 + ' ' + file2 + ')'
>>>> subprocess.check_call(cmd, shell=True)
>
> The command contained in cmd works ok from the bash prompt but not from
> Python code. In Python I get:
>
> /bin/sh: -c: line 0: syntax error near unexpected token `('
>
> I think the problem is that check_call is not using the bash shell.
I think your diagnosis is correct.
> So I
> also tried:
>
>>>> subprocess.check_call("bash", "-O", "extglob", "-c", cmd)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/local/lib/python3.4/subprocess.py", line 556, in check_call
> retcode = call(*popenargs, **kwargs)
> File "/usr/local/lib/python3.4/subprocess.py", line 537, in call
> with Popen(*popenargs, **kwargs) as p:
> File "/usr/local/lib/python3.4/subprocess.py", line 767, in __init__
> raise TypeError("bufsize must be an integer")
> TypeError: bufsize must be an integer
>
> Can anyone help me with this please?
Try specifying the shell explicitly:
check_call(["/bin/bash", "-c", cmd])
Or use a command that works with the default shell:
check_call("diff ... | head -n20", shell=True)
More information about the Python-list
mailing list