I'm trying to run my code in Python through Windows 10 cmd like this:
python3 flir_image_extractor.py -avg -i 'C:\\Users\\Daniel\\Desktop\\example.jpg'
The first thing it does is checking if the input path it's a file:
if not os.path.isfile(flir_img_filename):
raise ValueError("Input file does not exist or this user don't have permission on this file")
And that error is exactly what I'm getting when running the cmd command. My file exists and is located at the given path ("this pc" and "desktop", respectivelly), and explorer's set to show file extension:
I tried many other ways to input the path, as:
'C:\Users\Daniel\Desktop\example.jpg'
'C:/Users/Daniel/Desktop/example.jpg'
r'C:\Users\Daniel\Desktop\example.jpg'
Nothing worked so far.
This code works perfectly on Ubuntu, but I need to make sure it works on Windows in order to use PyInstaller for this OS. I'm using Python 3.9.5
You can check the complete code I'm using at https://github.com/Nervengift/read_thermal.py?fbclid=IwAR3FDfsIgSF5k-EKWSrAbnWO1oM-vWiBgDPrL3iOSSfUSexhlP1_BeVTUz4
. I added only the -avg option to match the use I want, but mimicking the other ones.
I thank you in advance.
EDIT:
I "coppyed as path" the file as sugested, and now there's the output:
C:\Users\Daniel\Desktop\venv_holder>python3 flir_image_extractor.py -avg -i "C:\Users\Daniel\Desktop\example.jpg"
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\venv_holder\flir_image_extractor.py", line 232, in <module>
fie.process_image(args.input)
File "C:\Users\Daniel\Desktop\venv_holder\flir_image_extractor.py", line 53, in process_image
if self.get_image_type().upper().strip() == "TIFF":
File "C:\Users\Daniel\Desktop\venv_holder\flir_image_extractor.py", line 66, in get_image_type
meta_json = subprocess.check_output(
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 424, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] O sistema não pode encontrar o arquivo especificado
Last line reads "System can't find the specified file"
If you're on windows, do not enclose your command line arguments in single quotes, they are passed as literals to your application. Either use doube quotes, or - if your path does not contain spaces - don't use quotes at all.
You probably could have figured that out yourself if you'd print out the filename as part of the error message.
Also: single quotes will not prevent a command line argument from being split if it contains spaces.
Related
I am trying to automate my build process. typically what I do is I go to a certain folder where my make scripts are at, open a CMD, type a command which is something like this "Bootstrap_make.bat /fic = Foo"
Now I want to do this from inside my python code. simple question is How to do this, i.e. how to; using python code. go to a certain path > and open a CMD > and execute the command"
here is what I've tried
def myClick():
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
subprocess.run(["bootstrap_make.bat","/fic= my_software_variant"])
However I git this error :
Traceback (most recent call last):
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call
return self.func(*args)
File "C:\E\ProductOne_Builder1.py", line 5, in myClick
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
You could use os.system().
https://docs.python.org/3/library/os.html
This code is assuming you are on Windows because you are running a .bat file and trying to open a CMD window.
import os
directory = './path/to/make/scripts'
buildCommand = './Bootstrap_make.bat'
buildArgs = '/fic = Foo'
# if you don't want a new cmd window you could do something like:
os.system('cd {} && {} {}'.format(directory, buildCommand, buildArgs))
# if you want a new cmd window you could do something like:
os.system('cd {} && start "" "{}" {}'.format(directory, buildCommand, buildArgs))
You can use subprocess.run, specifying the cwd (current working directory) kwarg.
I'm trying to sound process a wav file with python and pysndfx but getting this weird error. I've tried many different path formats and many different paths. Even thought os.path.isfile() returns true it still comes up with this error. Any help would be greatly appreciated.
from pysndfx import AudioEffectsChain
import os
in_file = os.getcwd() + "\\" + "a.mp3"
in_file = in_file.replace("\\", "//")#tried many things here, tried to it without any replacing
if os.path.isfile(in_file):
print("fileyes") #This returns true
else:
print("not a file")
print(in_file)
fs = 44100
fx = (AudioEffectsChain().
reverb().
delay().
phaser()
)
fx(in_file,"apro.mp3")
Here's the error
fileyes
E://PyEarTraning//Test//a.mp3
Traceback (most recent call last):
File "e:/PyEarTraning/Test/test.py", line 28, in <module>
fx(in_file,"E:\\PyEarTraning\\Test\\apro.mp3")
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pysndfx\dsp.py", line 368, in __call__
infile = FilePathInput(src)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pysndfx\sndfiles.py", line 29, in __init__
stdout, stderr = Popen(shlex.split(info_cmd, posix=False),
File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I can't comment (yet) so i'll ask here. Do you have your file in the same directory (folder) as the python program? If not then it won't work even if the file does actually exist somewhere. Try to copy or move both your file and program code into a new/the same folder.
I'm running the next piece of code to check the type of my file:
import subprocess as sub
output = sub.check_output(["file", "test.py"]).decode('ascii')
#output=sub.check_output(["file","C:/Users/Roger.That/PycharmProjects/test/test.py"]).decode('ascii')
and I keep getting the following error:
Traceback (most recent call last):
File "C:/Users/Roger That/PycharmProjects/test/test.py", line 2, in <module>
output = sub.check_output(["file", "test.py"]).decode('ascii')
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 395, in check_output
**kwargs).stdout
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
The full path of my test.py : "C:/Users/Roger.That/PycharmProjects/test/test.py"
output from gitbash :
$ file /c/Users/Roger\ That/PycharmProjects/test/test.py
/c/Users/Roger That/PycharmProjects/test/test.py: ASCII text, with CRLF line terminators
Any chance that it is related to the fact that I have a dot between "Roger" and "That"? Although it doesn't work also when I use the relative path (only the file name)
Update
I changed my user's dir name from Roger that to Roger.that, but it still didn't help:
python /c/Users/Roger.That/PycharmProjects/test/test.py
same error..
checked also :
import subprocess as sub
output = sub.check_output(["ls" "-l"])
got same error
I created from scratch the virtual environment and it solved my problem.
I am using windows and struggeling to get this work...
I can execute this in cmd.exe:
"C:\Program Files (x86)\Test 123\Test.exe" "H:\Test Test\file.txt" -f "doStuff"
but when I try to do it in python:
subprocess.call([r'"C:\Program Files (x86)\Test 123\Test.exe" "H:\Test Test\file.txt" -f "doStuff"'])
I get this error:
Traceback (most recent call last):
File "testing12.py", line 20, in <module>
subprocess.call([r'"C:\Program Files (x86)\Test 123\Test\Test.exe" "H:\Test Test\Folder\file.txt" -f "doStuff"'])
File "c:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "c:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "c:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
How can I execute it properly? Thanks.
If you're going to pass in an array, make it an actual array -- one argument per parameter, separated by commas. Otherwise you'll need to use shell=True, which has all the (generally undesirable) side effects of invoking a shell (and should just pass in your command string as a string, no array called for in that use case).
subprocess.call([
"C:\Program Files (x86)\Test 123\Test.exe",
"H:\Test Test\file.txt",
"-f", "doStuff"])
If you don't use commas between your strings, they're consolidated together.
I have the following (simplified) code:
with NamedTemporaryFile() as f:
f.write(zip_data)
f.flush()
subprocess.call("/usr/bin/7z x %s" % f.name)
It dies with the following error:
Traceback (most recent call last):
File "decrypt_resource.py", line 70, in <module>
unpack(sys.argv[2])
File "decrypt_resource.py", line 28, in unpack
print(subprocess.check_output(cmd))
File "/usr/lib/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
However, if I use NamedTemporaryFile(delete=False) and then print & execute the command, it works. What's wrong here?
My System is an ArchLinux with a 3.9.5-1-ARCH kernel.
You are using subprocess.call() incorrectly.
Pass in a list of arguments:
subprocess.call(["/usr/bin/7z", "x", f.name])
The argument is not handled by a shell and is not parsed out like a shell would do. This is a good thing as it prevents a security problem with untrusted command line arguments.
Your other options include using shlex.split() to do the whitespace splitting for you, or, as a last resort, telling subprocess to use a shell for your command with the shell=True flag. See the big warning on the subprocess documentation about enabling the shell.