繁体   English   中英

使用子进程时 Popen 出现 python 错误

[英]python error with Popen while using subprocess

我有我的 python 脚本

var1 = subprocess.Popen("pwd | grep 'home' ");
print var1

但它给了我错误

如果您希望 shell 正确解释 pipe,则需要添加shell=True

var1 = subprocess.Popen("pwd | grep 'home' ", shell=True)

(请注意,您不需要在行尾使用分号。)但这可能不会达到您的预期 - 它返回 Popen object 因此您需要检查var1.wait()是否返回0或不是。

一个更简单的方法,如果你只是想知道当前目录是否包含'home',是这样做:

if 'home' in os.getcwd():
    print "'home' is in the current working directory's path"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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