python exception error subprocess file missing - but what file? - python

i have a code running on python 2.7.3 (windows) and i try to run it on python 2.7.8(windows) and get the following error:
main : INFO ** Starting Main **
Traceback (most recent call last):
File "C:\wamp\www\prenderer\src\main.py", line 82, in <module>
nuke_process = launch_nuke()
File "C:\wamp\www\prenderer\src\main.py", line 31, in launch_nuke
query = subprocess.Popen(r"query process", stdout=subprocess.PIPE)
File "F:\python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "F:\python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>
what is wrong?

Pass shell=True argument:
query = subprocess.Popen(r"query process", stdout=subprocess.PIPE, shell=True)
or pass the command line argument as a list:
query = subprocess.Popen(["query", "process"], stdout=subprocess.PIPE)
Otherwise query process is recognized as a program instead of query.

Related

subprocess.run([‘cmd’, ‘args’]) returns FileNotFoundError

While coding an os extension with the “subprocesses” package, the FileNotFoundError seems to be re-occuring.
My code:
perm = “filename1 filename2”
subprocess.run(‘ren’, perm)
This returns the error:
Traceback (most recent call last):
File "main.py", line 46, in <module>
listeningfunc()
File "main.py", line 37, in listeningfunc
mv(listening.split(' ', 1)[1])
File "main.py", line 16, in mv
subprocess.run(['ren', perm])#, cwd=(os.getcwd()), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ren‘
I tried using
subprocess.run([’ren’, perm], cwd=(os.getcwd()), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
which seemed to resolve the issue (removes the error) but the function does not run. Can anyone help me with this? I’ve noticed that the package os also has similar issues.
Using OS package does not result in an error, but results in:
sh: 1: ren: not found
That's because ren is a cmd built-in. Several options:
perm = “filename1 filename2”
subprocess.run(‘ren’, perm, shell=True)
or prepend with cmd /c
perm = “filename1 filename2”
subprocess.run(['cmd','/c',‘ren’, "filename1","filename2"])
or way better: use native os functions as it's not worth to call ren when you can call os.rename
os.rename("filename1","filename2")
another way with shutil
shutil.move('filename1','filename2')

Python interface wifi.Cell.all(Interface) giving system error

I am trying to connect to the wireless network using wifi. As per the documents I have written the code, but it gives system error that file not found. As I can see the argument description of the Cell.all() is interface but we are providing literal as argument. What might be the problem?
import time
import datetime
from wifi import Cell, Scheme
while True :
print datetime.datetime.now()
print Cell.all('wlan0')
time.sleep(5)
>>>
2015-01-25 12:38:43.784000
Traceback (most recent call last):
File "D:\Documents\Development\Script\AutoLogon.py", line 9, in <module>
print Cell.all('wlan0')
File "C:\Python27\lib\site-packages\wifi\scan.py", line 29, in all
stderr=subprocess.STDOUT)
File "C:\Python27\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>
The python interface wifi was developed for Linux. I may be able to use the library using cygwin.

How to redirect stderr for subprocess module

Based on the answer provided here, I wanted to save the error as a string:
p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
output, errors = p.communicate()
However, it seems that redirecting stderr is not working:
>>> p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
errread, errwrite)
File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
What is the correct way to capture the error as a string?
The error message in the question does not come from the subprocess. It was generated before the subprocess execution. You cannot capture that error using stderr option.
Make sure there's ding program in the path.

Python subprocess.Popen Not finding Executable

I'm having issues with Python finding an available Executable on my Linux machine. My default PATH includes this Executable (svnlook) but when I run the python script the below function fails to find executable. Any ideas on how to fix this?
def command_output(cmd):
child = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output = child.communicate()[0]
return output, child.returncode
def get_author():
cmd = "svnlook author %s %s %s" % (svn_opt, svn_txn, svn_repo)
author, return_code = command_output(cmd)
return author.strip()
Error:
Traceback (most recent call last):
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 82, in <module>
author = get_author()
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 53, in get_author
author, return_code = command_output(cmd)
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 36, in command_output
child = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
File "/home/user/app/activepython-2.7.2.5_x86_64/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/home/user/app/activepython-2.7.2.5_x86_64/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Error: [Errno 2] No such file or directory
You probably want to provide the full path to the executable, e.g. /usr/bin/svnlook or /usr/local/bin/svnlook instead of just svnlook.
See this answer to a related question for details.
Try running it from the console. Make sure the permissions/executability is correct. Try os.system().

Compiling and Executing Java file in python

how can I open an java file in python?, i've search over the net and found this:
import os.path, subprocess
from subprocess import STDOUT, PIPE
def compile_java (java_file):
subprocess.check_call(['javac', java_file])
def execute_java (java_file):
cmd=['java', java_file]
proc=subprocess.Popen(cmd, stdout = PIPE, stderr = STDOUT)
input = subprocess.Popen(cmd, stdin = PIPE)
print(proc.stdout.read())
compile_java("CsMain.java")
execute_java("CsMain")
but then I got this error:
Traceback (most recent call last):
File "C:\Python33\lib\subprocess.py", line 1106, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\casestudy\opener.py", line 13, in <module>
compile_java("CsMain.java")
File "C:\casestudy\opener.py", line 5, in compile_java
subprocess.check_call(['javac', java_file])
File "C:\Python33\lib\subprocess.py", line 539, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Python33\lib\subprocess.py", line 520, in call
with Popen(*popenargs, **kwargs) as p:
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
>>>
the python file and java file is in the same folder, and I am using Python 3.3.2, how can I resolve this? or do you guys have another way on doing this?, any answer is appreciated thanks!
I think it isn't recognizing the javac command. Try manually running the command and if javac isn't a recognized command, register it in your PATH variable and try again.
Or you could just try typing the full pathname to the Java directory for javac and java.
you need to add path to your java file name. like this:
compile_java("C:\\path\to\this\CsMain.java")

Categories