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 Overflow
🌐
Python
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:
🌐
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....
🌐
Python Morsels
pythonmorsels.com › running-subprocesses-in-python
Running subprocesses in Python - Python Morsels
March 6, 2025 - Note that a subprocess (as I'm defining it) is not related to our process: it's not "forked" from our process, but instead is a separate application which is usually not even a Python process. To run a process, we can use the subprocess module's run function:
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - In this section, you’ll take a look at some of the most basic examples demonstrating the usage of the subprocess module. You’ll start by exploring a bare-bones command-line timer program with the run() function.
🌐
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.
Find elsewhere
🌐
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:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-subprocess-module
Python subprocess module - GeeksforGeeks
1 week ago - subprocess.run() executes a command and waits until it finishes. It returns an object that contains output, errors and return status of the command. Example: Here, a command is executed and both output and return code are printed.
🌐
Better Stack
betterstack.com › community › guides › scaling-python › python-subprocess
An Introduction to Python Subprocess | Better Stack Community
The subprocess module provides several functions for creating and interacting with subprocesses, with run() and Popen() being the most commonly used. Let's start with the simplest example using the high-level run() function, which was introduced in Python 3.5.
🌐
Medium
medium.com › @AlexanderObregon › how-to-use-pythons-subprocess-module-to-run-system-commands-ffdeabcb9721
How to Use Python’s subprocess Module to Run System Commands
November 12, 2024 - This example uses grep to filter lines containing the word hello from a string. The communicate method sends input to the command and retrieves its output. By default, subprocess.run does not execute commands in the shell.
🌐
Python Forum
python-forum.io › thread-39872.html
Using subprocess to execute complex command with many arguments
I have a shell script that is as follows: LOAD_RRD=sysmon.rrd DISK_RRD=disk.rrd DISKTEMP_RRD=disktemp.rrd rrdtool graph load1h.png \ -Y -u 1.1 -l 0 -L 5 -v 'Load' -w 700 -h 200 -t 'Load stats - `/bin/date`' \ --start end-'1h' --x-grid MIN...
🌐
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 )
🌐
Earthly
earthly.dev › blog › python-subprocess
How to Use Python's Subprocess Module - Earthly Blog
July 11, 2023 - Learn how to use Python's subprocess module to run external commands, capture and process outputs, redirect output to files, and more. This tut...
🌐
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
    
Published   Nov 16, 2013
Version   0.0.8
🌐
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 il­lus­trate how Python subprocess works. To do this, we first import the subprocess and sys modules and then execute a simple request. The cor­re­spond­ing 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)
🌐
Python Land
python.land › home › interaction with the operating system › python subprocess: run external commands
Python Subprocess: Run External Commands • Python Land Tutorial
October 30, 2024 - Learn how to execute external command with Python using the subprocess library. With examples to run commands, capture output, and feed stdin