Python 中的哪个命令等效
我有以下代码,用于检查系统上是否存在程序类转储。该程序仅返回一个空白。
cmd = ["which","class-dump"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
print process.stdout.read()
此代码始终返回空白。从技术上讲,它应该可以正常工作吗?
I have the following code which i am using to check if the program class-dump exists on a system. The program only returns a blank.
cmd = ["which","class-dump"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
print process.stdout.read()
This code always returns a blank. Technically it should work right ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在我的机器上尝试了以下操作,它运行得很好。
这是输出
I tried the following on my machine and its working perfectly.
this is the output
which
是一个内置的 shell。您需要将shell=True
传递给 Popen。which
is a shell built-in. You need to passshell=True
to the Popen.嗯,看起来类转储不存在(在 python 解释器使用的 PATH 中)
Hmmm, it looks like the class-dump does not exists (in the PATH used by your python interpreter)
如果您改用
communicate
是否有效?请参阅此警告:另外,您的脚本是否可能在您的 shell 之外的另一个
PATH
下运行?转储 os.environ["PATH"] 并检查。Does it work if you use
communicate
instead? See this warning:Also, is it possible that your script runs under another
PATH
than your shell? Dumpos.environ["PATH"]
and check.此代码以编程方式执行与 shell
which
命令相同的操作,只是它不“查找”符号链接。为了支持这一点,您可以增强isx
函数。使用示例:
This code does the same thing as the shell
which
command programatically, except that it does not "find" symbolic links. To support that, you can enhance theisx
function.Example use: