Curly braces in python Popen

Running a subprocess will not correctly handle curly braces

# 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 another machine with python 2.7.2. Both systems use bash shells.

You have a reason and how can I fix it?

EDIT:

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

ls src/{t,p}*.cpp
src/tonamep.cpp src/pch.cpp
+4
source share
3 answers

shell=Trueruns /bin/shthat does not support this syntax. Indicate bashexplicitly:

from subprocess import check_call

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

In your case, Popen runs correctly, an error message is reported from ls. It should give the same error when executing the command:

ls src/{t,p}*.cpp

in the terminal.

+1

, .

0
source

Source: https://habr.com/ru/post/1533510/


All Articles