Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.run not working in windows - The system cannot find the file specified [duplicate]

I'm trying to use the subprocess module in visual sutdio code but I keep getting an error. Here is the code:

import subprocess
subprocess.run("dir") 

or

import subprocess
subprocess.run("dir","/p")

and the error is:

Traceback (most recent call last):
  File "e:\Dropbox (Personal)\My Python\external\extrprg.py", line 2, in <module>
    subprocess.run("dir")
  File "E:\Users\klo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "E:\Users\klo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "E:\Users\klo\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

I'd expect python to run dir and give me a list of the folders in the directory

like image 665
kavm Avatar asked Oct 30 '25 02:10

kavm


1 Answers

dir is a command in cmd.exe, which means you want to do:

subprocess.run(['cmd.exe', '/c', 'dir'])
like image 63
Hai Vu Avatar answered Oct 31 '25 17:10

Hai Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!