2024-04-29 17:50:04 +01:00
|
|
|
---
|
|
|
|
id: cfr4
|
|
|
|
tags: [python, shell]
|
|
|
|
created: Monday, April 29, 2024
|
|
|
|
---
|
|
|
|
|
|
|
|
# Invoking the shell_in_Python
|
|
|
|
|
|
|
|
```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
|
|
|
```
|