eolas/neuron/57f4d44e-6e5d-4bdf-a12c-b7dac10ca90d/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()
```