eolas/neuron/f8981ab1-f587-4bd7-a1a8-aa934a221168/Invoking_the_shell_in_Python.md

25 lines
402 B
Markdown
Raw Normal View History

2024-12-09 18:34:15 +00: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()
```