-2

from subprocess import call call(["ls", "-l"])

this returns

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

Dustin
  • 1

1 Answers1

1

First of all you are using a linux command on Windows OS like Marco Bonelli mentioned.

You should change the ls command to Windows equivalent like this:

subprocess.call(["dir","/Q"],shell = True)

We specify shell=True because dir command is built-in to shell, otherwise you would get the same error.

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Spiros Gkogkas
  • 432
  • 5
  • 11
  • If I run the corrected program as a script, the console only restarts itself. However, if I enter the code as a series of commands on the shell, it returns the information as expected. Why is that?thanks for the answer, I literally started python a week ago and didn't expected so many answers so fast – Dustin Mar 24 '21 at 16:57
  • @Dustin Try running the **.py** file as `python .\file.py` from cmd, if you are simply running the file as `.\file.py` (depending **assoc** of **.py** files) it will probably run and it will print the expected output but you will not be able to see it. You can try out to print an input line at the end of the script and you will see that there is an output. So you can try either that or just add `subprocess.call("pause", shell=True)`.However there are better solutions to do such tasks and some of them are mentioned at the above comments. – Spiros Gkogkas Mar 24 '21 at 17:36