FileNotFoundError: [WinError 2] The system cannot find the file specified - python

I am trying to open a shortcut for Notepad called Notepad.lnk.
Because I want to learn how to open applications so I can use the pygui library to manipulate them.
Got FileNotFoundError
I got this error:
Traceback (most recent call last):
File "C:\Users\{my_name}\PycharmProjects\texture-gen\main.py", line 185, in <module>
subprocess.run(['start', shortcut_path], check=True)
File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
Code
This is my python code:
if __name__ == '__main__':
import os
import subprocess
cwd = os.getcwd()
shortcut_path = os.path.join(cwd, 'Notepad.lnk')
if not os.path.exists(shortcut_path):
print('Error: "Notepad.lnk" shortcut not found')
subprocess.run(['start', shortcut_path], check=True)
Debugging
I have checked the name of the shortcut is Notepad.lnk and is in the current working directory.
Why is this not working?

Related

I get FileNotFound error while using psutil library

I'm trying to run and kill site.py python script every 5 minutes using webreset.py script.
webreset.py:
from psutil import Process, Popen
from time import sleep
pid = 0
while True:
if pid:
Process(pid).terminate()
process = Popen('python3 site.py')
pid = process.pid
sleep(300)
But when I run webreset.py on Ubuntu 20.04, I get the following Error:
Traceback (most recent call last):
File "webreset.py", line 7, in <module>
process = Popen('python3 site.py')
File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 1316, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/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: 'python3 site.py'
I run the script using this command :
sudo python3 webreset.py
You need to separate the command and arguments.
process = Popen(['python3', 'site.py'])
Otherwise it is looking for a command with the filename "python3 site.py"
using this:
Popen(['python3', 'site.py'])
instead of:
process = Popen('python3 site.py')
fixed my problem.

Error while running bash command in python using subprocess: [WinError 2] The system cannot find the file specified

I am trying to execute the following bash command in python using the subprocess python package but its throwing me an error.
the bashCommand variable in the following code stores the bash command i'm trying to execute
def desc_calc():
bashCommand = "java -Xms2G -Xmx2G -Djava.awt.headless=true -jar ./PaDEL-Descriptor/PaDEL-Descriptor.jar -removesalt -standardizenitro -fingerprints -descriptortypes ./PaDEL-Descriptor/PubchemFingerprinter.xml -dir ./ -file descriptors_output.csv"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
upon running the code, i get the following error:
2023-01-25 17:27:06.101 Uncaught app exception
Traceback (most recent call last):
File "C:\Users\oo_wa\.conda\envs\streamlit\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 563, in _run_script
exec(code, module.__dict__)
File "C:\Users\oo_wa\OneDrive\Desktop\BUSI_Dashboard\pages\2_Drug Discovery.py", line 82, in <module>
desc_calc()
File "C:\Users\oo_wa\OneDrive\Desktop\BUSI_Dashboard\pages\2_Drug Discovery.py", line 44, in desc_calc
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
File "C:\Users\oo_wa\.conda\envs\streamlit\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\oo_wa\.conda\envs\streamlit\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

I keep getting PermissionError: [WinError 5] Access is denied on Pytesserect

I know this has been answered but nothing works, I run my code
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = r"C:\\Users\\aidan\\AppData\\Local\\Tesseract-OCR"
image = cv2.imread(r'C:\Users\aidan\OneDrive\Desktop\download.jpg')
cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(image))
cv2.imshow('Result',image)
cv2.waitKey()
and I always get this error
[ WARN:0#0.043] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('download.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] Access is denied
how can I fix this?
You have dropped the executable in the cmd-variable:
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\aidan\AppData\Local\Tesseract-OCR\tesseract.exe"

Python subprocess.Popen, .run, or .call would not work on bootsect.exe

I tried invoking bootsect.exe with subprocess.Popen, subprocess.run, subprocess.call, or even os.system but I keep getting the error below...
File "<stdin>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\username\AppData\Local\Programs\Python\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
For instance, I get this error if use the code below...
import subprocess
subprocess.Popen('bootsect')
I can run diskpart or cmd using Popen but not bootsect for some reason. Any thoughts?

How to solve subprocess.run() execution error?

this is my code:
import subprocess
completed = subprocess.run(['ls', '-1'])
print('returncode:', completed.returncode)
this is the error i am getting:
Traceback (most recent call last):
File "getDataFromOpenImages_snowman.py", line 3, in <module>
completed = subprocess.run(['ls', '-1'])
File "C:\Users\pavan\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\pavan\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\pavan\AppData\Local\Programs\Python\Python38\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
Question: what is the problem with my code?

Categories

Resources