Python way to create a new folder and create files in it, or automatically create it if the folder path does not exist

Reference: https://www.python100.com/html/S2IT74V832KF.html
In Python, if there is no path, you can use the os.makedirs() method to create a new folder or directory. Below we will introduce in detail how Python creates a new folder and creates files in it from many aspects.

1. Create a new folder

New folders or directories can be easily created using the os.makedirs() method:

import os

path = "new_folder"
os.makedirs(path)

This code will create a folder named "new_folder" in the current working directory. But what if you want to create a new folder under a specific path?

import os

# 打开指定目录
path = "C:/Users/user/Desktop"
os.chdir(path)

# 创建新文件夹
new_folder = "new_folder"
os.makedirs(new_folder)

First, use the os.chdir() method to open the specified directory, and then use the os.makedirs() method to create a folder named "new_folder" in the directory.

2. Create files in a new folder

New files can be easily created in a new folder using the open() method:

import os

path = "new_folder"
os.makedirs(path)

file = open(path + "/new_file.txt", "w")
file.write("Hello World!")
file.close()

This code will create a new folder "new_folder", create a file called "new_file.txt" in it, and write "Hello World!"

3. Determine whether the folder exists

Before creating a new folder, we need to make sure that the folder does not exist. You can use the os.path.exists() method to check if a folder exists:

import os

path = "new_folder"

if not os.path.exists(path):
    os.makedirs(path)
    print("Folder created")
else:
    print("Folder already exists")

This code will check if the folder "new_folder" exists. It will create a new folder and print "Folder created" if it does not exist; if it already exists, it will print "Folder already exists".

4. Only create folders, not files.
If you only want to create a folder, but not create files in it, you can use the os.mkdir() method:

import os

path = "new_folder"
os.mkdir(path)

The os.mkdir() method is similar to the os.makedirs() method, but does not create intermediate folders in the path. The os.mkdir() method throws an exception if the directory in the path does not exist.

5. Create multi-level folders

If you want to create multi-level folders, you can pass a path with multiple directories to the os.mkdir() or os.makedirs() method:

import os

path = "new_folder/sub_folder/child_folder"
os.makedirs(path)

This code will create a multi-level directory named "new_folder/sub_folder/child_folder" in the current working directory.

6. Summary

This article details the different ways Python can create a new folder and create files within it. First, we learned to use the os.makedirs() method to create a new folder or directory. Next, we discussed how to create a file in a new folder, how to tell if a folder exists, and how to create only a folder without creating a file. Finally, we discussed how to create multi-level folders. I hope this article can help you better understand the operation of creating folders in Python.

Guess you like

Origin blog.csdn.net/qq_21237549/article/details/131939646