When we use program development, an excellent project is inseparable from directory management. We usually store the same type of files in a specified directory. In this section, we will learn how to create a directory in the program.
There is a function os.mkdir() to create a directory in the os module. Its syntax format is:
os.mkdir(path)
Path is the name of the directory we want to create. You can use an absolute path or a relative path.
Before creating a directory, let's first learn a function os.path.exists() to determine whether a directory exists. Its syntax format is:
os.path.exists(path)
Path is the path used to determine whether it exists. If the path exists, it returns True, otherwise it returns False.
Look at the code below:
importosifos.path.exists('test.txt'): #The file exists in the current directory folder print('This directory exists')
The output is:
The directory exists
Then we combine the above judgment methods to create the directory. The code is as follows:
importosifnotos.path.exists('First-level directory'):os.mkdir('First-level directory')print('Creation completed')else:print('Directory already exists')
Then we can see the directory under the current folder, as shown below:
When we execute this program again, it will output:
Directory already exists
If we need to create a multi-level directory, we can use the os.makedirs() function in the os module. Its syntax format is:
os.makedirs(path)
We explain this multi-level directory creation method through a program.
The code is as follows:
importosnow=os.getcwd()os.makedirs('onekikizizi')print('Created successfully')
After running, let's take a look at a directory that exists in our current directory.
Through the creation of multi-level directories, the path we fill in the function is a relative path. It creates multiple directories in sequence under the current directory. First, a directory named one is created, and then the kiki directory is created in the one directory, and then A directory named zizi is created again.
To delete a directory, use the os.rmdir() function in the os module, but this function is limited to no subdirectories or files in the directory to be deleted. Its syntax structure is as follows:
os.rmdir(path)
You can use relative paths or absolute paths when deleting. The code is as follows:
We first write a program to create 2 directories, and then delete them through two path methods. First create the directory code:
importosifnotos.path.exists('First-level directory'):os.mkdir('First-level directory')os.mkdir('First-level directory kiki')print('Creation completed')
The files are as follows:
The deletion code is as follows:
importosos.rmdir('C:/Users/Qingyan/PycharmProjects/untitled1/teach/First-level directory')os.rmdir('First-level directory kiki')print('Deletion completed')
The files are as follows:
Regarding directory management, we must pay attention to whether the path is correct. When developing a complete project, try to use absolute paths to ensure the correctness of the program. You can practice the following example.
The topic is: Create 5 directories and each directory contains 5 directories. The directory names are 128 random letters.
The solution link is: https://blog.dotcpp.com/a/65341