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 - Stderr output of the child process if it was captured by run(). Otherwise, None. This is always bytes when stderr output was captured regardless of the text=True setting. It may remain None instead of b'' when no stderr output was observed. Added in version 3.3. Changed in version 3.5: stdout and stderr attributes added ... Subclass of SubprocessError, raised when a process run by check_call(), check_output(), or run() (with check=True) returns a non-zero exit status.
Discussions

How subprocess run() works?
Running python sys.executable in pycharm shows it uses virtual environment interpreter (venv) PS C:\Users\SJ\Desktop\Programs\Python\PyTest> python Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" ... More on discuss.python.org
🌐 discuss.python.org
7
0
June 21, 2024
How do you keep a subprocess running after the python script ends on Windows?
This isn't really possible, because any subprocesses must be terminated when the parent process terminates. However, you can use pythonw to run a script in the background, without opening a window. The easiest way to use it is to change the extension from .py to .pyw. More on reddit.com
🌐 r/learnpython
16
5
February 5, 2022
subprocess.run: simple but powerful lib to execute external processes

Do you know about python-sh?

http://amoffat.github.io/sh/

More on reddit.com
🌐 r/Python
4
7
October 22, 2013
subprocess run a exe it one in a while loop
You can start the process using Popen and than poll to check if it is running. p = subprocess.Popen([yourapp.exe]) while p.poll() is None: ... # do something while process is running ... # do something when process is terminated More on reddit.com
🌐 r/learnpython
2
1
February 23, 2022
People also ask

What does subprocess run do?
Subprocess runs the command, waits for the procedure to complete, and then returns a "Completed process" artifact.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
Is subprocess a standard Python library?
With the help of the subprocess library, we can run and control subprocesses right from Python.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - Let's take a look at when to use the Python subprocess module. The subprocess module can be used to automate various system tasks, such as running backups, starting and stopping services, and scheduling cron jobs. For example, you can use the subprocess module to run the cp command to create ...
🌐
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 - For example, sys.executable might be a path like /usr/local/bin/python. subprocess.run is given a list of strings consisting of the components of the command we are trying to run.
🌐
Dataquest
dataquest.io › blog › python-subprocess
Python Subprocess: The Simple Beginner's Tutorial (2023)
February 19, 2025 - In this article, we'll demonstrate how to use the subprocess module in Python to run different subprocesses during a regular Python script.
🌐
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
Find elsewhere
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › retrieving-the-output-of-subprocesscall-in-python
Retrieving the output of subprocess.call() in Python - GeeksforGeeks
July 23, 2025 - The subprocess.run() function allows you to capture the output by setting the capture_output parameter to True.
🌐
Earthly
earthly.dev › blog › python-subprocess
How to Use Python's Subprocess Module - Earthly Blog
July 11, 2023 - The subprocess module is built ... subprocess module, you can call the run() function with the syntax: subprocess.run(command) to run an external command....
🌐
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
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - Python’s subprocess module allows you to run shell commands and manage external processes directly from your Python code. By using subprocess, you can execute shell commands like ls or dir, launch applications, and handle both input and output ...
🌐
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 - Understanding these techniques ... to execute a command is by using the subprocess.run method. This function executes the command and waits for it to complete....
🌐
Python Morsels
pythonmorsels.com › running-subprocesses-in-python
Running subprocesses in Python - Python Morsels
March 6, 2025 - Note that a subprocess (as I'm ... which is usually not even a Python process. To run a process, we can use the subprocess module's run function:...
🌐
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
Learn how to use Python’s `subprocess` module, including `run()` and `Popen()` to execute shell commands, capture output, and control processes with real-world examples.
🌐
Better Stack
betterstack.com › community › guides › scaling-python › python-subprocess
An Introduction to Python Subprocess | Better Stack Community
Learn Python’s `subprocess` module to run commands, capture output, handle errors, and build safer, more flexible automation scripts.
🌐
Medium
medium.com › @techclaw › python-subprocess-run-ad2fcedde2d5
Python Subprocess Run. Introduction | by TechClaw | Medium
August 8, 2023 - The subprocess.run() function is a part of the subprocess module introduced in Python 3.5. It provides a simple and efficient way to spawn new processes, execute system commands, and interact with them.
🌐
Replit
replit.com › home › discover › how to use 'subprocess' in python
How to use 'subprocess' in Python | Replit
2 weeks ago - Python's subprocess module lets you run and manage external commands from your scripts.
🌐
Python
docs.python.org › 3 › library › asyncio-subprocess.html
Subprocesses — Python 3.14.3 documentation
February 23, 2026 - For processes created with create_subprocess_shell(), the return code reflects the exit status of the shell itself (e.g. /bin/sh), which may map signals to codes such as 128+N. See the documentation of the shell (for example, the Bash manual’s Exit Status) for details. Standard asyncio event loop supports running subprocesses from different threads by default.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-subprocess-module
Python subprocess module - GeeksforGeeks
1 week ago - The subprocess module is used to run external programs or system commands from Python. It allows to execute commands, capture their output and interact with other processes.