Operations with the OS Module

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to interact with the operating system using the os module in Python. You will create a multi-level directory, move and rename a directory, and create a file.

👀 Preview

$ python3 os_operations.py
## The "/home/labex/project/" directory has the following structure:
├── test1
│ └── first
└── test2
└── labex.txt

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a multi-level directory using the os.makedirs() function
  • How to move and rename a directory using the os.rename() function
  • How to create a file and write content to it using the open() function and the with statement

🏆 Achievements

After completing this project, you will be able to:

  • Understand the basic operations you can perform on directories and files using the os module in Python
  • Apply these skills to automate common file and directory management tasks in your future projects
  • Enhance your Python programming skills and gain experience in working with the operating system

Create a Multi-Level Directory

In this step, you will learn how to create a multi-level directory using the os module in Python. Follow the steps below to complete this step:

  1. Open the os_operations.py file in your code editor.
  2. Import the os module at the beginning of the file:
import os
  1. Inside the create_and_move() function, add the following code to create the multi-level directory test1/first/second under the /home/labex/project directory:
os.makedirs("/home/labex/project/test1/first/second")

This will create the test1 directory, then the first directory inside test1, and finally the second directory inside first.

Move and Rename a Directory

In this step, you will learn how to move and rename a directory using the os module. Follow the steps below to complete this step:

  1. Inside the create_and_move() function, add the following code to move the second directory from the subdirectory to the /home/labex/project directory and rename it to test2:
os.rename("/home/labex/project/test1/first/second", "/home/labex/project/test2")

This will move the second directory from test1/first/ to the /home/labex/project directory and rename it to test2.

Create a File

In this step, you will learn how to create a file and write content to it using the os module. Follow the steps below to complete this step:

  1. Inside the create_and_move() function, add the following code to create a labex.txt file in the test2 directory and write the content "Hello, LabEx!":
with open("/home/labex/project/test2/labex.txt", "w") as file:
    file.write("Hello, LabEx!")

This will create a new file named labex.txt in the test2 directory and write the content "Hello, LabEx!" to the file.

Run the Script

To run the script and create the directories and file, follow these steps:

  1. Save the os_operations.py file.
  2. Open a terminal or command prompt and navigate to the directory where you saved the os_operations.py file.
  3. Run the script using the following command:
python3 os_operations.py

This will execute the create_and_move() function and create the directory structure and file as specified in the previous steps.

After running the script, you should see the following file structure in the /home/labex/project directory:

├── test1
│   └── first
└── test2
    └── labex.txt

Congratulations! You have successfully completed the project using the os module in Python.

âœĻ 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