views:

36

answers:

1

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
+2  A: 
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()
unutbu
are you supposed to always use 'ls' and put the command line call in the second part? The other way with shlex that you posted seems to be working..
Rick
@Rick: `ls` was just an example; you should replace it with whatever real command you are trying to run.
unutbu
I got `subprocess.Popen(shlex.split('ls -l'))` to work for basic commands, like `ls-l` but for my command, which is to start a server, I'm still getting '[Errno 2] No such file or directory', when the same exact command works using os.popen. Its too out of context to get into the command call here (its for selenium web server) but I'm wondering if there is any difference as far as what the system is receiving between the different ways of making the calls as I can't figure out why its doing this
Rick
@Rick: Perhaps try giving the full path to any files referenced in the command. Maybe something like `proc=subprocess.Popen(shlex.split('java -jar /full/path/to/selenium-server.jar'))` where of course, you should replace `/full/path/to` to whatever is appropriate.
unutbu