Python 3 – OS File/Directory Methods
Python is a popular programming language because of its versatility, and it can be used in many different applications. One of the areas where it is commonly used is in managing files and directories on a computer’s operating system. In this article, we will discuss the methods that Python 3 provides for working with files and directories on a computer.
OS Module Introduction
The OS module in Python provides a way to interact with the operating system in which the program is running. It allows you to perform tasks like reading and writing files, listing directories, checking for the existence of files, and a lot more.
To use the OS module in Python, you simply need to import it at the beginning of your Python script:
import os
Now that the module is imported, we can start exploring its methods.
Get the Current Directory
You can get the current directory of your program using the getcwd()
method.
import os
# Get the current working directory
current_dir = os.getcwd()
print("Current directory:", current_dir)
Output:
Current directory: C:\Users\User\Desktop
Changing Directories
You can change the current directory using the chdir()
method.
import os
# Get the current working directory
current_dir = os.getcwd()
print("Directory before changing:", current_dir)
# Changing the current directory to C drive
os.chdir("C:\\")
print("Directory after changing:", os.getcwd())
Output:
Directory before changing: C:\Users\User\Desktop
Directory after changing: C:\
In the above example, we first get the current working directory and print it out to the console. We then change the directory to the C drive using the chdir() method, and then print out the new directory after the change.
Checking if a File or Directory Exists
You can check if a file or a directory exists in the current directory using the path.exists()
method.
import os
# Checking if the file 'example.txt' exists in the current directory
if os.path.exists("example.txt"):
print("File exists")
else:
print("File does not exist")
# Checking if the directory 'test_folder' exists in the current directory
if os.path.exists("test_folder"):
print("Directory exists")
else:
print("Directory does not exist")
Output:
File does not exist
Directory exists
Creating a Directory
You can create a new directory using the mkdir()
method.
import os
# Creating a new directory named 'test_folder'
os.mkdir("test_folder")
print("Directory created successfully!")
Output:
Directory created successfully!
Renaming a File or Directory
You can rename a file or a directory using the rename()
method.
import os
# Renaming the file 'example.txt' to 'new_example.txt'
os.rename("example.txt", "new_example.txt")
print("File renamed successfully!")
# Renaming the directory 'test_folder' to 'new_test_folder'
os.rename("test_folder", "new_test_folder")
print("Directory renamed successfully!")
Output:
File renamed successfully!
Directory renamed successfully!
Listing Directories
You can list the directories and files in a directory using the listdir()
method.
import os
# Listing all the files and directories in the current directory
dir_list = os.listdir()
for item in dir_list:
print(item)
Output:
new_test_folder
new_example.txt
Removing a File or Directory
You can remove a file or a directory using the remove()
and rmdir()
method respectively.
import os
# Removing the file 'new_example.txt'
os.remove("new_example.txt")
print("File removed successfully!")
# Removing the directory 'new_test_folder'
os.rmdir("new_test_folder")
print("Directory removed successfully!")
Output:
File removed successfully!
Directory removed successfully!
Conclusion
Python’s OS module provides lots of functionalities that will enable you to interact with the operating system effectively. In this article, we have covered some of the essential methods that you will use when working with files and directories.
We hope this article has helped you in exploring some of the crucial OS methods available in Python. Happy Coding!