0

Even when I run simple code like this:

import subprocess
subprocess.run("dir")

I get an error

C:\Users\mrlol\Desktop\Test>py test.py
Traceback (most recent call last):
  File "C:\Users\mrlol\Desktop\Test\test.py", line 2, in <module>
    subprocess.run("dir")
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 546, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1022, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1491, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

Edit 1 I have added shell=True, but still this issue remains(

    C:\Users\mrlol\Desktop\Test>py test.py
Traceback (most recent call last):
  File "C:\Users\mrlol\Desktop\Test\test.py", line 2, in <module>
    subprocess.run("dir", shell=True)
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 546, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1022, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\mrlol\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1491, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

I have used another program and I had an issue. Now I see that it is subprocess causing the issue.

Mr lolua
  • 1
  • 1
  • See top solution in this thread here: https://stackoverflow.com/a/44230826/5401481 – sedeant Oct 28 '22 at 12:48
  • `dir` is command built-in in `cmd.exe` - so it may need to use `shell=True` - like `subprocess.run("dir", shell=True)` . But I don't use Windows so I can't test it. – furas Oct 28 '22 at 14:24

0 Answers0