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.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/FileHandlingGroup -.-> python/file_operations("`File Operations`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/conditional_statements -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/tuples -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/function_definition -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/importing_modules -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/standard_libraries -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/file_opening_closing -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/file_reading_writing -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/file_operations -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/os_system -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} python/build_in_functions -.-> lab-302691{{"`Automatic Spaceship Detection Script`"}} end

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.

Summary

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

Other Python Tutorials you may like