Automatic Spaceship Detection Script

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to create an automated script that can detect the disk usage of the current system. This script is designed to be used by the Galactic Empire's spaceship maintenance engineer to ensure that the basic data of the spaceship is normal before takeoff.

👀 Preview

$ python auto_check_script.py
['20480', '131', '20348']

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a Python script named flight_check.py that can detect the total disk space, used disk space, and unused disk space of the current system.
  • How to implement the auto_check_script() function in the auto_check_script.py file to run the flight_check.py script and capture its output.
  • How to test the auto_check_script() function to ensure that it is working correctly.

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to use Python and the os library to create an automated script for system detection.
  • Implement a function that can run a Python script and capture its output.
  • Test and verify the functionality of an automated script.

Create the flight_check.py File

In this step, you will learn how to create the flight_check.py file using Python code.

  1. Open your text editor and create a new file named flight_check.py.
  2. In the flight_check.py file, add the following code:
import shutil

def check_disk_usage():
    total, used, free = shutil.disk_usage("/")
    print(total // (2**20))  ## total disk space
    print(used // (2**20))   ## used disk space
    print(free // (2**20))   ## unused disk space

if __name__ == "__main__":
    check_disk_usage()

This code defines a check_disk_usage() function that uses the shutil.disk_usage("/") method to obtain the total, used, and free disk space on the current operating system. The function then prints these values in MB units.

Implement the auto_check_script() Function

In this step, you will learn how to implement the auto_check_script() function in the auto_check_script.py file.

  1. Open the auto_check_script.py file located in the /home/labex/project directory.
  2. In the auto_check_script() function, add the following code:
import os

def auto_check_script():
    result = os.popen("python flight_check.py").read().splitlines()
    return result

if __name__ == "__main__":
    file = open("flight_check.py", "w")
    file.write(
        """
import shutil

def check_disk_usage():
    total, used, free = shutil.disk_usage("/")
    print(total // (2**20))  ## total disk space
    print(used // (2**20))   ## used disk space
    print(free // (2**20))   ## unused disk space

if __name__ == "__main__":
    check_disk_usage()
    """
    )
    file.close()

    print(auto_check_script())

This code uses the os.popen() function to run the flight_check.py script and capture its output. The output is then split into a list of lines, which is returned by the auto_check_script() function.

Test the auto_check_script() Function

In this step, you will test the auto_check_script() function to ensure that it is working correctly.

  1. Run the auto_check_script.py file using the following command:
python auto_check_script.py

This should output the total, used, and unused disk space in MB units, like this:

['20480', '131', '20348']
  1. Verify that the output matches the actual disk usage on your system.

Congratulations! You have completed the project. The auto_check_script() function in the auto_check_script.py file is now ready to be used by the Galactic Empire's spaceship maintenance engineer.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like