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
5 days 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.
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:
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
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....
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 ... at runtime. ... We execute an external Python script 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())
Dataquest
dataquest.io › blog › python-subprocess
Python Subprocess: The Simple Beginner's Tutorial (2023)
February 19, 2025 - In fact, this is the same as passing /usr/local/bin/python -c print('This is a subprocess') to the command line. Most of the code in this article will be in this format because it's easier to show the features of the run function. However, you can always call another Python script containing the same code. Also, both times we used the run function in the examples above, the first string in args refers to the path to the Python executable.
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:
Computer Science Atlas
csatlas.com › python-subprocess-run-stdin
Python 3: Standard Input with subprocess.run() — Computer Science Atlas
September 3, 2021 - from subprocess import run with open( 'myfiles.tar.gz', 'rb' ) as f: data = f.read() run( [ 'tar', 'xzf', '-' ], input=data )
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
IONOS
ionos.com › digital guide › websites › web development › python subprocess
How to use Python subprocess to execute external commands and programs
June 27, 2025 - We will now use this function for our first small example to illustrate how Python subprocess works. To do this, we first import the subprocess and sys modules and then execute a simple request. The corresponding code looks like this: import subprocess import sys result = subprocess.run([sys.executable, "-c", "print('hello')"])python
Linux find Examples
queirozf.com › entries › python-3-subprocess-examples
Python 3 Subprocess Examples
November 26, 2022 - run() returns a CompletedProcess object instead of the process return code. A CompletedProcess object has attributes like args, returncode, etc. subprocess.CompletedProcess
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
Instead, the function is passed to Popen as the preexec_fn argument so it is run after the fork() inside the new process, before it uses exec() to run the shell. import os import signal import subprocess import tempfile import time import sys script = '''#!/bin/sh echo "Shell script in process $$" set -x python signal_child.py ''' script_file = tempfile.NamedTemporaryFile('wt') script_file.write(script) script_file.flush() proc = subprocess.Popen(['sh', script_file.name], close_fds=True, preexec_fn=os.setsid, ) print 'PARENT : Pausing before sending signal to child %s...' % proc.pid sys.stdout.flush() time.sleep(1) print 'PARENT : Signaling process group %s' % proc.pid sys.stdout.flush() os.killpg(proc.pid, signal.SIGUSR1) time.sleep(3)