eolas/neuron/233bf7c7-59e9-471c-8275-895571468b75/Invoking_the_shell_in_Python.md

25 lines
402 B
Markdown
Raw Normal View History

2024-10-19 11:00:03 +01:00
---
id: cfr4
tags: [python, shell]
created: Monday, April 29, 2024
---
# Invoking the shell in Python
```py
import subprocess
try:
process = subprocess.run(
["ls", "-la"],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return process.stdout
except subprocess.CalledProcessError as e:
return e.stderr.strip()
```