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 thewithstatement
🏆 Achievements
After completing this project, you will be able to:
- Understand the basic operations you can perform on directories and files using the
osmodule 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:
- Open the
os_operations.pyfile in your code editor. - Import the
osmodule at the beginning of the file:
import os
- Inside the
create_and_move()function, add the following code to create the multi-level directorytest1/first/secondunder the/home/labex/projectdirectory:
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:
- Inside the
create_and_move()function, add the following code to move theseconddirectory from the subdirectory to the/home/labex/projectdirectory and rename it totest2:
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:
- Inside the
create_and_move()function, add the following code to create alabex.txtfile in thetest2directory 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:
- Save the
os_operations.pyfile. - Open a terminal or command prompt and navigate to the directory where you saved the
os_operations.pyfile. - 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.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



