Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 235
Rick Avatar asked Sep 30 '10 22:09

Rick


People also ask

What is the difference between subprocess call and Popen?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

What is subprocess popen ()?

The subprocess. popen() is one of the most useful methods which is used to create a process. This process can be used to run a command or execute binary. The process creation is also called as spawning a new process which is different from the current process.

How do you call a subprocess in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.

What are some like commands for subprocess in Linux?

like: os.system, os.spawn*, os.popen*, popen2.* commands. Let’s start looking into the different functions of subprocess. Run the command described by “args”. Note, the default value of the shell argument is False.

How to execute system commands using Python subprocess?

We will use Python subprocess module to execute system commands. We can run shell commands by using subprocess.call () function. See the following code which is equivalent to the previous code.

What is the difference between subprocess Popen and pipe in Python?

PIPE indicates that a new pipe to the child should be created. The default setting is “None”, which means that no redirection will occur. for stdout. handled by the Popen class. subprocess.popen is replacing os.popen. Let’s get started with some real examples. way of doing it. such as Popen and PIPE, then it is enough to only import those.

What is the use of OS Popen in Linux?

The os.popen method opens a pipe from a command. This pipe allows the command to send its output to another command. The output is an open file that can be accessed by other programs.


1 Answers

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()
like image 199
unutbu Avatar answered Oct 12 '22 12:10

unutbu