Popen 无法处理在 shell 中运行的命令

发布于 2024-10-08 17:20:34 字数 1432 浏览 4 评论 0原文

我已经阅读了之前关于同一论点的问题,但我真的还没有弄清楚。

我正在尝试从命令行运行一个没有问题的命令:

xyz@klm:~/python-remoteWorkspace/PyLogParser/src:18:43>ush -o PPP -p PRD -n log 'pwd'
6:43PM PPP:prd:lgsprdppp:/ama/log/PRD/ppp

但是当我在 python 中执行相同操作时,我总是收到错误:

stringa = Popen(["ush -o PPP -p PRD -n log 'pwd'"], stdout=PIPE, stdin=PIPE).communicate()[0]

这里是错误。

Traceback (most recent call last):   File "getStatData.py", line 134, in <module>
    retrieveListOfFiles(infoToRetList)   File "getStatData.py", line 120, in retrieveListOfFiles
    stringa = Popen(["ush -o PPP -p PRD -n log 'pwd'"], stdout=PIPE, stdin=PIPE).communicate()[0]   File "/opt/python-2.6-64/lib/python2.6/subprocess.py", line 595, in __init__
    errread, errwrite)   File "/opt/python-2.6-64/lib/python2.6/subprocess.py", line 1092, in _execute_child
    raise child_exception OSError: [Errno 2] No such file or directory

我也尝试过不同的解决方案,

stringa = Popen(["ush", "-o", "PPP", "-p" "PRD", "-n", "log", '"pwd"'], stdout=PIPE, stdin=PIPE).communicate()[0]

但似乎没有任何效果。我也尝试过将绝对路径放入 ush 但什么也没有...... 有人可以解释一下我做错了什么吗?

预先感谢,AM。

编辑 : 我发生了一件奇怪的事情,当我这样做时

which ush

,我得到了

ush: aliased to nocorrect /projects/aaaaaaa/local/ush/latest/ush.py

但是为什么它会起作用???

!!!谢谢大家的解答!!!

I have already read the previous questions posted on the same argument but I really haven't figured it out yet.

I am trying to run a command that works without issues from the command line :

xyz@klm:~/python-remoteWorkspace/PyLogParser/src:18:43>ush -o PPP -p PRD -n log 'pwd'
6:43PM PPP:prd:lgsprdppp:/ama/log/PRD/ppp

but when I do the same in python I always get errors :

stringa = Popen(["ush -o PPP -p PRD -n log 'pwd'"], stdout=PIPE, stdin=PIPE).communicate()[0]

Here the error.

Traceback (most recent call last):   File "getStatData.py", line 134, in <module>
    retrieveListOfFiles(infoToRetList)   File "getStatData.py", line 120, in retrieveListOfFiles
    stringa = Popen(["ush -o PPP -p PRD -n log 'pwd'"], stdout=PIPE, stdin=PIPE).communicate()[0]   File "/opt/python-2.6-64/lib/python2.6/subprocess.py", line 595, in __init__
    errread, errwrite)   File "/opt/python-2.6-64/lib/python2.6/subprocess.py", line 1092, in _execute_child
    raise child_exception OSError: [Errno 2] No such file or directory

I've tried also different solutions like

stringa = Popen(["ush", "-o", "PPP", "-p" "PRD", "-n", "log", '"pwd"'], stdout=PIPE, stdin=PIPE).communicate()[0]

but nothing seems to work. I have also tried to put the absolute path to ush but nothing...
Can somebody please explain me what am I doing wrong ?

Thanks in advance, AM.

EDIT :
I have a strange thing happening, when I do

which ush

I get

ush: aliased to nocorrect /projects/aaaaaaa/local/ush/latest/ush.py

But why is it working then ???

!!! Thank you all for the answers !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你穿错了嫁妆 2024-10-15 17:20:34
Popen(["ush", "-o", "PPP", "-p", "PRD", "-n", "log", "pwd"])

应该是对的。 shell 命令中 'pwd' 周围的额外引号使其成为单个参数,但引号实际上并未传递。由于您已经拆分了参数,因此请保留多余的引号。


显然(在OP的更新中)ush是一个shell别名。因此,它只在壳内膨胀;在其他任何地方,它都行不通。自己扩展一下:

Popen(["nocorrect", "/projects/aaaaaaa/local/ush/latest/ush.py",
       "-o", "PPP", "-p", "PRD", "-n", "log", "pwd"])
Popen(["ush", "-o", "PPP", "-p", "PRD", "-n", "log", "pwd"])

should be right. The extra quoting around 'pwd' in the shell command makes it a single argument, but the quotes aren't actually passed along. Since you're already splitting the arguments, leave the extra quotes out.


Apparently (in an update from OP) ush is a shell alias. Thus, it only expands in the shell; anywhere else, it won't work. Expand it yourself:

Popen(["nocorrect", "/projects/aaaaaaa/local/ush/latest/ush.py",
       "-o", "PPP", "-p", "PRD", "-n", "log", "pwd"])
怀里藏娇 2024-10-15 17:20:34

如果您系统上的 ush 是别名,则 popen 将不起作用。 popen 需要一个可执行文件作为第一个参数:绝对路径或 PATH 中某个内容的名称。

If ush on your system is an alias, popen won't work. popen requires an executable file as the first parameter: either an absolute path or the name of something that is in your PATH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文