python Popen中的花括号 [英] Curly Braces in python Popen

查看:130
本文介绍了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

同一程序将在使用python 2.7.2的另一台计算机上运行.两种系统都使用bash shell.

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?

直接从命令行调用命令将返回正确的结果:

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运行不支持此语法的/bin/sh.明确指定bash:

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

这篇关于python Popen中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆