Curly Braces in python Popen

懵懂的女人 提交于 2020-02-11 06:50:05

问题


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

回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/22659579/curly-braces-in-python-popen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!