subprocess call errors in python - python

I have been having problems with subprocess call.
I wrote a very simple code (test.py) that simply prints "Hello….."
Then I did the following:
/sw/bin/python2.7
import subprocess
call (["test.py"])
I got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/sw/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/sw/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/sw/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I can get it to work with os.system but I am keen to learn this subprocess method.
Where am I going wrong?
The test.py being called is in the same folder.

Try this:
subprocess.call(["python", "test.py"])

Related

Python subprocess using Idle

I am new to python scripting. I am trying to run subprocess methods on Python Idle and getting these errors:
import subprocess
subprocess.check_output("ls")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Please help me out to resolve this error.
Thank you
seems like you are running windows. Windows has no "ls" command, so you get a FileNotFoundError exception. I am not familiar with windows and python, but try "dir" instead of "ls", that should work.

Python: I want to execute shell commands using python script

I want to execute shell command "objdump" using python for my research work
I have used subprocess.call("command") to execute linux command but its not working.
Sample code which i have tried is
import subprocess
subprocess.call("date")
after the execution
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
You must do this:
subprocess.call('date', shell=True)
Actually, Shell does allow you to access to global variable and programs that are in $PATH and your shell.
Have you tried ?
import subprocess
subprocess.call("date", shell = True)

can any body help? I get this error, and I cant solve it?

I am tring to run a python file and it start but after that give me this error. What I have to do to fixed this error, I am beginner with python.
Traceback (most recent call last):
File "./mininet_multicast_pox.py", line 318, in <module>
mcastTest(topo, False, hosts)
File "./mininet_multicast_pox.py", line 53, in mcastTest
pox_process = Popen(pox_arguments, stdout=fnull, stderr=fnull, shell=False, close_fds=True)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
You have to provide an absolute path to you file.
Without that, the subprocess module can't find it.
The os module provides routine for that.

Error from tshark and python

I am trying to execute this simple tshark command through python but I am not able to succeed and I don't understand the error
OS : Windows 7
Python 3.3
Code is
from subprocess import Popen
f = open("C:\\folder2\\test.txt","w")
Popen(["tshark" ,"-r","C:\\folder2\\fvido.pcap","-qz", "io,stat,0"],stdout=f)
and the error I get is
Traceback (most recent call last):
File "C:/Users/pc/Google Drive/Python/tshark1.py", line 6, in <module>
Popen(["tshark" ,"-r","C:\\folder2\\fvido.pcap","-qz", "io,stat,0"],stdout=f)
File "C:\Python33\lib\subprocess.py", line 820, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
raise WindowsError(*e.args)
FileNotFoundError: [WinError 2] The system cannot find the file specified

python subprocess throws error "no such file or direcrory"

s=subprocess.Popen(['/home/karthik/Downloads/stanford-parser-2011-06-08/lexparser.csh','-'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1213, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I'm sure this file exists and open() with this filename works.Why am i getting this error ? i use python 2.7
Make sure csh is installed and is in /bin/csh (otherwise edit the command after the shebang in lexparser.sh).

Categories

Resources