How to create a new folder in the current directory using Python?

In Python, you can use the mkdir() function in the os module to create a new folder in the current directory (or a specified directory).

Here is an example code:

import os

# 指定新文件夹的名称
new_folder = "my_folder"

# 使用当前目录路径和新文件夹名称拼接成新的路径
path = os.path.join(os.getcwd(), new_folder)

# 使用 mkdir() 函数创建新文件夹
os.mkdir(path)

print("文件夹创建成功!")

In the above code, first, we use the os.getcwd() function to obtain the current directory path, then use the os.path.join() function to combine the current directory path with the new folder name into a new path. Finally, we use the os.mkdir() function to create a folder in the new path.

Running the above code will create a folder named my_folder in the current directory.

bannerAds