Python Creating files and directories with Python
Creating Files and Directories with Python
In this article, we’ll learn how to create files and directories using Python. Creating files and directories is a very common programming task, and Python provides built-in functions and modules that make it easy to accomplish these tasks.
Read more: Python Tutorial
Creating Files
In Python, we can use the built-in function open()
to create a new file. open()
accepts two parameters: the first is the file path and file name, and the second is the operation mode. The following is an example of creating a file:
file = open("example.txt", "w")
file.close()
In the above example, we use the open()
function to create a new file named “example.txt.” The second parameter, “w,” specifies write mode; other modes, such as “r,” can also be used for read mode. After creating the file, we use the close()
function to close it.
Creating Directories
You can create directories using Python’s os
module or the pathlib
module. The following example uses the os module to create a directory:
import os
if not os.path.exists(“example_dir”):
os.makedirs(“example_dir”)
In the example above, the os.path.exists() function is used to check if the target directory already exists. If it does not exist, the os.makedirs() function is used to create the directory.
The following example uses the pathlib
module to create a directory:
from pathlib import Path
Path("example_dir").mkdir(parents=True, exist_ok=True)
In the above example, Path("example_dir")
creates a Path
object and then uses the mkdir()
method to create the directory. parents=True
automatically creates the parent directory if it doesn’t exist; exist_ok=True
prevents an exception if the directory already exists.
Creating Multiple Directories
If you need to create multiple directories, you can use the os.makedirs()
function and the Path.mkdir()
method. The following example creates multiple directories:
import os
if not os.path.exists(“parent_dir/child_dir”):
os.makedirs(“parent_dir/child_dir”)
from pathlib import Path
Path(“parent_dir/child_dir”).mkdir(parents=True, exist_ok=True)
In the above example, we use slash marks in the directory path to represent multiple directories.
Complete Example of Creating Files and Directories
Here is a complete example demonstrating how to create files and directories and check their existence using the os.path
module:
import os
def create_file(file_path):
if not os.path.exists(file_path):
file = open(file_path, "w")
file.close()
print(f"File {file_path} created successfully")
else:
print(f"File {file_path} already exists")
def create_directory(dir_path):
if not os.path.exists(dir_path):
os.makedirs(dir_path)
print(f"Directory {dir_path} created successfully")
else:
print(f"Directory {dir_path} already exists")
file_path = "example.txt"
dir_path = "example_dir"
create_file(file_path)
create_directory(dir_path)
Running the above code will create a file named “example.txt” and a directory named “example_dir”. If the file or directory already exists, a corresponding prompt will be printed.
Summary
Creating files and directories in Python is very simple. We can use the open()
function and the os
module or the pathlib
module to accomplish these tasks. Remember to select the appropriate operation mode when creating files and check whether the directory already exists when creating it to avoid duplicate creation. I hope this article helps you learn how to create files and directories in Python.