简体   繁体   中英

Python, using subprocess.Popen to make linux command line call? I'm getting “[Errno 2] No such file or directory”

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.

EDIT: 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 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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