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.
🌐
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....
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
os.popen vs subprocess.run for simple commands

About 13 years ago, the older, deprecated per-os popen2/popen3/popen4 calls were removed, and os.popen() was implemented in terms of subprocess.Popen (just like subprocess.run is).:

https://github.com/python/cpython/commit/c2f93dc2e42b48a20578599407b0bb51a6663d09#diff-405b29928f2a3ae216e45afe9b5d0c60

So, I don't think the current os.popen() is deprecated anymore (I can't see any indication that it is; that was for the older versions which were removed).

Therefore, I think you can safely use it if you want. But, keep in mind that the popen() concept was pretty Unix specific, and isn't obvious to everyone that it's running a subprocess, whereas that should be fairly obvious from subprocess.run().

You could consider making a dictionary of keyword args (that you re-use), and passing that to subprocess.run(), if it's just the length of the calling lines that is concerning you:

runargs = {'shell': True, 'capture_output': True, 'text': True }
output = subprocess.run('<command>', **runargs).stdout
More on reddit.com
🌐 r/learnpython
5
4
June 20, 2020
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
🌐
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 - The -c component is a python command line option that allows you to pass a string with an entire Python program to execute. In our case, we pass a program that prints the string ocean. You can think of each entry in the list that we pass to subprocess.run as being separated by a space. For example, [sys.executable, "-c", "print('ocean')"] translates roughly to /usr/local/bin/python -c "print('ocean')".
🌐
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:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-subprocess-module
Python subprocess module - GeeksforGeeks
1 week ago - ... import subprocess try: output = subprocess.check_output(["python", "--version"], text=True) print(output) except subprocess.CalledProcessError: print("Command failed") ... subprocess.call() runs a command and returns its exit status.
🌐
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.
Find elsewhere
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - So, for the rest of the pipe examples, only UNIX-based examples will be used, as the basic mechanism is the same for both systems. They’re not nearly as common on Windows, anyway. If you want to let the shell take care of piping processes into one another, then you can just pass the whole string as a command into subprocess: ... >>> import subprocess >>> subprocess.run(["sh" , "-c", "ls /usr/bin | grep python"]) python3 python3-config python3.8 python3.8-config ...
🌐
Red Hat
redhat.com › en › blog › use-python-subprocess-bash
2 practical ways to use the Python subprocess module
November 23, 2022 - This example uses the usecase2 folder in the subprocess_demo repo. The Bash script in this example (check_ceph.sh) accesses the backend Ceph cluster for OpenShift Data Foundation via rsh and runs several Ceph commands to determine the cluster's health and architecture. [ Do you know the difference between Red Hat OpenShift and Kubernetes? ] If you do not have an active OpenShift Data Foundation cluster, you can run the Python script using the -i output.txt flag in the terminal.
🌐
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
🌐
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...
🌐
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....
🌐
Better Stack
betterstack.com › community › guides › scaling-python › python-subprocess
An Introduction to Python Subprocess | Better Stack Community
This example shows how subprocess.run() executes a shell command and returns a result object, including the command’s exit code. This return code is useful for understanding the outcome of the command—especially with tools like grep, where: ... With the basics in place, let’s look at how to actually capture the output of a command. In many cases, you won’t just want to run a command—you’ll want to capture and use its output in your Python code.
🌐
Linux find Examples
queirozf.com › entries › python-3-subprocess-examples
Python 3 Subprocess Examples
November 26, 2022 - import subprocess subprocess.run(["ls","foo bar"], check=True) # ------------------------------------------------------------------- # CalledProcessError Traceback (most recent call last) # ----> 1 subprocess.run(["ls","foo bar"], check=True) # /usr/lib/python3.6/subprocess.py in run(input, ...
🌐
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
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 12_useful_modules › subprocess.html
subprocess - Python for network engineers
Example of subprocess module use (subprocess_run_basic.py file): import subprocess reply = subprocess.run(['ping', '-c', '3', '-n', '8.8.8.8']) if reply.returncode == 0: print('Alive') else: print('Unreachable') ... $ python subprocess_run_basic.py PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
🌐
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
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)
🌐
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 - Learn how to execute system commands in Python using the subprocess module, with examples of capturing output, handling errors, and running pipelines.