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 OverflowDataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - In this article, we'll explore the basics of the subprocess module, including how to run external commands, redirect input and output, and handle errors. Whether you're a beginner or an experienced Python developer, this tutorial will provide you with the knowledge you need to use the subprocess module effectively in your projects.
Videos
09:57
Using the Python subprocess Module: Gettting Started & Using ...
19:01
Python Tutorial: Calling External Commands Using the Subprocess ...
13:17
Python 3 Programming Tutorial - Subprocess module - YouTube
52:46
Practical introduction to Python's subprocess module - YouTube
13:34
Python Standard Library: Subprocess - YouTube
10:03
Run Windows Programs Using Python Subprocess Code Example 2023 ...
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())
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 subprocess module is a powerful part of the Python standard library that lets you run external programs and inspect their outputs easily. In this tutorial, you have learned to use subprocess.run to control external programs, pass input to them, parse their output, and check their return codes.
W3Schools
w3schools.com › python › ref_module_subprocess.asp
Python subprocess Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... import subprocess result = subprocess.run(['echo', 'Hello from Emil'], capture_output=True, text=True) print(result.stdout) Try it Yourself »
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.
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
Better Stack
betterstack.com › community › guides › scaling-python › python-subprocess
An Introduction to Python Subprocess | Better Stack Community
This article will show you how to use subprocess effectively in your Python applications. You'll learn to run external commands, communicate with other programs, and properly handle errors. ... Better Stack lets you see inside any stack, debug any issue, and resolve any incident. Explore more ... To get the most out of this tutorial...
Python 101
python101.pythonlibrary.org › chapter19_subprocess.html
Chapter 19 - The subprocess Module — Python 101 1.0 documentation
In this code example, we create an args variable to hold our list of arguments. Then we redirect standard out (stdout) to our subprocess so we can communicate with it. The communicate method itself allows us to communicate with the process we just spawned. We can actually pass input to the ...
Top answer 1 of 6
1
Your example code has some errors and isn’t runnable as-is.
The process’s communicate method waits for the subprocess to finish before returning the output.
To read the output while the subprocess is running, read from the process’s stdout:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, …
2 of 6
0
Thanks!
So I added a output list to capture each line of the output just before the print statement, which is great.
But how do I catch errors? Say my process had errors, I would like that same error we see in terminal to be in the variable error.
If I add err = process.stderr.readline(), it give…