The stack trace suggests you're using Windows as the operating system. ls not something that you will typically find on a Windows machine unless using something like CygWin.
Instead, try one of these options:
# use python's standard library function instead of invoking a subprocess
import os
os.listdir()
# invoke cmd and call the `dir` command
import subprocess
subprocess.run(["cmd", "/c", "dir"])
# invoke PowerShell and call the `ls` command, which is actually an alias for `Get-ChildItem`
import subprocess
subprocess.run(["powershell", "-c", "ls"])
Answer from Czaporka on Stack OverflowPython
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
1 week ago - To determine if the shell failed to find the requested application, it is necessary to check the return code or output from the subprocess. A ValueError will be raised if Popen is called with invalid arguments. check_call() and check_output() will raise CalledProcessError if the called process returns a non-zero return code. All of the functions and methods that accept a timeout parameter, such as run() and Popen.communicate() will raise TimeoutExpired if the timeout expires before the process exits.
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - Using the Python subprocess module ... tasks and integrate other programs with your Python code. For example, you can use the subprocess module to run a shell command, like ls or ping, and get the output of that command in your Python code....
Videos
09:57
Using the Python subprocess Module: Gettting Started & Using ...
02:03
Using subprocess to Run Python (Video) – Real Python
19:01
Python Tutorial: Calling External Commands Using the Subprocess ...
01:40
Using the Python subprocess Module (Overview) (Video) – Real Python
How to run shell commands with python? python subprocess ...
03:14
python subprocess run example - YouTube
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-subprocess-to-run-external-programs-in-python-3
How To Use subprocess to Run External Programs in Python 3 | DigitalOcean
July 30, 2020 - As an example, this pattern could be useful if we wanted to raise an exception in the event that we run git ls-files in a directory that wasn’t actually a git repository. We can use the check=True keyword argument to subprocess.run to have an exception raised if the external program returns a non-zero exit code:
Codecademy
codecademy.com › article › python-subprocess-tutorial-master-run-and-popen-commands-with-examples
Python Subprocess Tutorial: Master run() and Popen() Commands (with Examples) | Codecademy
Note: This code’s output will ... named my_python_file.py in this example. ... The subprocess.run() function is used here to execute another Python file....
Top answer 1 of 2
8
The stack trace suggests you're using Windows as the operating system. ls not something that you will typically find on a Windows machine unless using something like CygWin.
Instead, try one of these options:
# use python's standard library function instead of invoking a subprocess
import os
os.listdir()
# invoke cmd and call the `dir` command
import subprocess
subprocess.run(["cmd", "/c", "dir"])
# invoke PowerShell and call the `ls` command, which is actually an alias for `Get-ChildItem`
import subprocess
subprocess.run(["powershell", "-c", "ls"])
2 of 2
3
ls is not a Windows command. The windows analogue is dir, so you could do something like
import subprocess
subprocess.run(['cmd', '/c', 'dir'])
However, if you're really just trying to list a directory it would be much better (and portable) to use something like os.listdir()
import os
os.listdir()
or pathlib
from pathlib import Path
list(Path().iterdir())
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 12_useful_modules › subprocess.html
subprocess - Python for network engineers
In [7]: result = subprocess.run('ls -ls *md', shell=True) 4 -rw-r--r-- 1 vagrant vagrant 56 Jun 7 19:35 ipython_as_mngmt_console.md 4 -rw-r--r-- 1 vagrant vagrant 1638 Jun 7 19:35 module_search.md 4 -rw-r--r-- 1 vagrant vagrant 277 Jun 7 19:35 README.md 4 -rw-r--r-- 1 vagrant vagrant 49 Jun 7 19:35 version_control.md · Another feature of run() If you try to run a ping command, for example, this aspect will be visible:
Linux find Examples
queirozf.com › entries › python-3-subprocess-examples
Python 3 Subprocess Examples
November 26, 2022 - See Popen() vs call() vs run() This causes the python program to block until the subprocess returns. from subprocess import Popen p = Popen(["ls","-lha"]) p.wait() # 0
Simplilearn
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
December 15, 2025 - Learn about Python's subprocess module for executing external commands. Discover how to manage processes and handle inputs/outputs efficiently.
Address 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
The default is to run the command directly. import subprocess # Command with shell expansion subprocess.call('echo $HOME', shell=True)
PyPI
pypi.org › project › subprocess.run
subprocess.run · PyPI
>>> from subprocess import run >>> run('uname -r').stdout 3.7.0-7-generic >>> run('uname -a').status 0 >>> print run('rm not_existing_directory').stderr rm: cannot remove `not_existing_directory': No such file or directory >>> print run('ls -la', 'wc -l') 14
» pip install subprocess.run
Python Module of the Week
pymotw.com › 3 › subprocess
subprocess — Spawning Additional Processes
March 18, 2018 - The command line arguments are passed as a list of strings, which avoids the need for escaping quotes or other special characters that might be interpreted by the shell. run() returns a CompletedProcess instance, with information about the process like the exit code and output. $ python3 subprocess_os_system.py index.rst interaction.py repeater.py signal_child.py signal_parent.py subprocess_check_output_error_trap_output.py subprocess_os_system.py subprocess_pipes.py subprocess_popen2.py subprocess_popen3.py subprocess_popen4.py subprocess_popen_read.py subprocess_popen_write.py subprocess_run_check.py subprocess_run_output.py subprocess_run_output_error.py subprocess_run_output_error_suppress.py subprocess_run_output_error_trap.py subprocess_shell_variables.py subprocess_signal_parent_shell.py subprocess_signal_setpgrp.py returncode: 0