Python 中的哪个命令等效

发布于 2024-12-06 16:19:54 字数 218 浏览 1 评论 0原文

我有以下代码,用于检查系统上是否存在程序类转储。该程序仅返回一个空白。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心房敞 2024-12-13 16:19:54

我在我的机器上尝试了以下操作,它运行得很好。

import subprocess
cmd = ["which","java"]
process =  subprocess.Popen(cmd, stdout=subprocess.PIPE)
print process.communicate()

这是输出

('/usr/bin/java\n', None)

I tried the following on my machine and its working perfectly.

import subprocess
cmd = ["which","java"]
process =  subprocess.Popen(cmd, stdout=subprocess.PIPE)
print process.communicate()

this is the output

('/usr/bin/java\n', None)
调妓 2024-12-13 16:19:54

which 是一个内置的 shell。您需要将 shell=True 传递给 Popen。

which is a shell built-in. You need to pass shell=True to the Popen.

哽咽笑 2024-12-13 16:19:54

嗯,看起来类转储不存在(在 python 解释器使用的 PATH 中)

Hmmm, it looks like the class-dump does not exists (in the PATH used by your python interpreter)

不甘平庸 2024-12-13 16:19:54

如果您改用 communicate 是否有效?请参阅此警告:

警告

使用communicate()而不是.stdin.write、.stdout.read或
.stderr.read 以避免由于任何其他操作系统管道而导致的死锁
缓冲区填满并阻塞子进程。

另外,您的脚本是否可能在您的 shell 之外的另一个 PATH 下运行?转储 os.environ["PATH"] 并检查。

Does it work if you use communicate instead? See this warning:

Warning

Use communicate() rather than .stdin.write, .stdout.read or
.stderr.read to avoid deadlocks due to any of the other OS pipe
buffers filling up and blocking the child process.

Also, is it possible that your script runs under another PATH than your shell? Dump os.environ["PATH"] and check.

白衬杉格子梦 2024-12-13 16:19:54

此代码以编程方式执行与 shell which 命令相同的操作,只是它不“查找”符号链接。为了支持这一点,您可以增强 isx 函数。

import os.path, itertools
isx = lambda program: lambda folder: (lambda p: \
    os.access(p, os.X_OK) and os.path.isfile(p))(os.path.join(folder, program))

def which(program):
  try:
    return itertools.ifilter(isx(program), os.environ['PATH'].split(':')).next()
  except StopIteration:
    raise ValueError, 'no executable file named %s found in search path' % (program,)

使用示例:

>>> which('ls')
'/bin'
>>> which('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in which
ValueError: no executable file named foo found in search path

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 the isx function.

import os.path, itertools
isx = lambda program: lambda folder: (lambda p: \
    os.access(p, os.X_OK) and os.path.isfile(p))(os.path.join(folder, program))

def which(program):
  try:
    return itertools.ifilter(isx(program), os.environ['PATH'].split(':')).next()
  except StopIteration:
    raise ValueError, 'no executable file named %s found in search path' % (program,)

Example use:

>>> which('ls')
'/bin'
>>> which('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in which
ValueError: no executable file named foo found in search path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文