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 - 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.
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())
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
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
subprocess.run: simple but powerful lib to execute external processes
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
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
Videos
19:01
Python Tutorial: Calling External Commands Using the Subprocess ...
09:57
Using the Python subprocess Module: Gettting Started & Using ...
13:34
Python Standard Library: Subprocess - YouTube
02:03
Using subprocess to Run Python (Video) – Real Python
02:59
python subprocess run command - YouTube
r/Python on Reddit: Calling a System/Shell command with Python ...
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.
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
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
Top answer 1 of 7
1
I need to stress that subprocess.run() itself does not search for “python” on Windows, unlike how it’s implemented on POSIX. It just lets the system API find “python”, which is where the peculiar behavior with the application directory comes into play. If, for example, the virtual environment gets c…
2 of 7
0
I am using PyCharm and I don’t get that result.
[pycharm_executable_location]
Maybe it was how you had set up PyCharm during installation?
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.
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.