5
votes

Running subprocess won't handle curly braces correctly

# Python 2.7.4

import subprocess
subprocess.Popen('ls src/*.cpp',shell=True): 
src/tonemap.cpp src/pch.cpp

subprocess.Popen('ls src/{t,p}*.cpp', shell=True)
ls: cannot access src/{p,t}*.cpp: No such file or directory

The same program will work on a different machine with python 2.7.2. Both systems use bash shells.

Do you the reason and how can I fix it?

EDIT:

Invoking the command directly from the command line returns the correct result:

ls src/{t,p}*.cpp
src/tonamep.cpp src/pch.cpp
3
And how can I roll back from 2.7.4 to 2.7.2 on a Linux Mint? - memecs
Your second example works for me...what shell is on both systems? - Tom Fenech
@TomFenech They are both bash shells - memecs
and what happens in the shell when you run ls src/{t,p}*.cpp on the "broken" system, does it work? - Tom Fenech
Yes it works, output is 'tonemap.cpp pch.cpp' as expected. - memecs

3 Answers

9
votes

shell=True runs /bin/sh that doesn't support this syntax. Specify bash explicitly:

from subprocess import check_call

check_call('ls src/{t,p}*.cpp', shell=True, executable='/bin/bash')
1
votes

In your case, Popen executed correctly, error is reported from ls. It should give same error when you execute the command:

ls src/{t,p}*.cpp

in terminal.

0
votes

The other machine uses a different shell that doesn't handle that syntax.