-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubprocess-logger-module
More file actions
26 lines (22 loc) · 1 KB
/
subprocess-logger-module
File metadata and controls
26 lines (22 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import subprocess
import logging
logging.basicConfig(level=logging.INFO, format='| %(levelname)-15s | %(asctime)s | %(name)s | %(lineno)-4s :: %(message)s')
logger = logging.getLogger("test")
# Define the command you want to execute
command = [
'grep -rl "org" *',
'ls',
'pwd'
]
# Run the command
The stdout=subprocess.PIPE parameter captures the output of the command, which you can then access using result.stdout.
for cmd in command:
result = subprocess.run(cmd, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Print the output
print(result.stdout)
print(result.stderr, end='')
# end='' used to remove newline
#subprocess.run() is used to run the specified command.
#The shell=True parameter indicates that the command should be run in a shell.
#The check=True parameter raises a CalledProcessError if the command returns a non-zero exit code, indicating an error.
#The text=True parameter ensures that the output is returned as a string.