Python,使用subprocess.Popen进行Linux命令行调用?我收到"[Errno 2]没有这样的文件或目录" [英] Python, using subprocess.Popen to make linux command line call? I'm getting "[Errno 2] No such file or directory"

查看:381
本文介绍了Python,使用subprocess.Popen进行Linux命令行调用?我收到"[Errno 2]没有这样的文件或目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循我可以找到的有关subprocess.Popen的信息,因为我想进行linux命令行调用. ".我不是要打开文件,所以我不理解此错误,当我使用常规操作系统时,它可以正常工作(尽管还有其他一些与等待进程完成有关的问题). .popen.

I'm trying to follow the info I can find about subprocess.Popen as I want to make a linux command line call.. I am trying as below but am getting the error "[Errno 2] No such file or directory". I'm not trying to open a file so I don't understand this error, and it works fine (although with other issues relating to waiting for the process to finish when I don't want it to) when I use a regular os.popen.

我似乎无法弄清楚如何正确执行此操作,感谢您提供任何建议.

I can't seem to figure out how to do this properly, any advice is appreciated.

我正在使用的命令是复杂且可变的,将其包含在上下文中太过上下文了,我认为足以说明该代码在我使用os.popen时有效,而在我使用时不起作用新方法,所以不,"linux命令行调用"显然不是我正在使用的调用

THE COMMAND I AM USING IS COMPLEX AND VARIABLIZED, it would be too out-of-context to include it here, I think its suffice to say that the code works when I use os.popen and not when I do the new way, so no, the "linux command line call" is obviously not the call I am using

subprocess.Popen([r"linux command line call"])
>>> [Errno 2] No such file or directory

推荐答案

import subprocess    
proc=subprocess.Popen(['ls','-l'])  # <-- Change the command here
proc.communicate()

Popen需要一个字符串列表.第一个字符串通常是要运行的程序,其后是参数.有时,当命令很复杂时,使用shlex.split为您编写列表很方便:

Popen expects a list of strings. The first string is typically the program to be run, followed by its arguments. Sometimes when the command is complicated, it's convenient to use shlex.split to compose the list for you:

import shlex
proc=subprocess.Popen(shlex.split('ls -l'))  
proc.communicate()

这篇关于Python,使用subprocess.Popen进行Linux命令行调用?我收到"[Errno 2]没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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