eolas/zk/Invoking_the_shell_in_Python.md

25 lines
402 B
Markdown
Raw Normal View History

2024-04-29 17:50:04 +01:00
---
id: cfr4
tags: [python, shell]
created: Monday, April 29, 2024
---
2024-10-18 20:00:02 +01:00
# Invoking the shell in Python
2024-04-29 17:50:04 +01:00
```py
import subprocess
try:
2024-04-29 18:00:04 +01:00
process = subprocess.run(
["ls", "-la"],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
2024-06-18 07:45:05 +01:00
return process.stdout
2024-04-29 17:50:04 +01:00
2024-04-29 18:00:04 +01:00
except subprocess.CalledProcessError as e:
return e.stderr.strip()
2024-04-29 17:50:04 +01:00
```