简体   繁体   中英

Curly Braces in python Popen

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

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')

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.

另一台机器使用不处理该语法的不同shell。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM