Rick
Rick

Reputation: 17013

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

Upvotes: 6

Views: 22665

Answers (1)

unutbu
unutbu

Reputation: 880887

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

Upvotes: 17

Related Questions