eolas/neuron/8e6dee36-9f5f-47c8-b9f6-5160559ef056/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()
```