Python | check log file size with Subprocess module

Python subprocess module is very useful is in automating Linux system administration tasks. Below is the code snippet for getting log file size in Python with subprocess module.

  • Create a file with name "" and write below code.
    # Import Subprocess module
    from subprocess import Popen, PIPE
    # Command to get file size of yum.log
    cmd = "du -sh /var/log/yum.log"
    # Popen module to execute command 
    output = Popen(cmd, stdout=PIPE, shell=True)
    for line in output.stdout.readlines():
  • Run the python file with below command and check the output

  • Category: Python