Python subprocess module is very useful is in automating Linux system administration tasks.
Below is the code snippet for getting size of all log files inside "/var/log" directory in Python with subprocess module.
Create a file with name "gcptutorials.py" and write below code.
#!/usr/bin/python3
# Import Subprocess module
from subprocess import Popen, PIPE
# Command to get size of all log files in "/var/log" directory
cmd = 'find /var/log/ -name "*.log" -exec du -sh {} \;'
output = Popen(cmd, stdout=PIPE, shell=True)
for line in output.stdout.readlines():
print(line.decode('utf-8'))
Run the python file with below command and check the output