Summarize the usage of mkdir and makedirs:
1.mkdir (path [, mode])
Function: Create a directory that can be a relative or absolute path, the default mode is 0777.
If the directory has multiple levels, the last level is created. If the parent directory of the last level directory does not exist, a oserror is thrown,
2.makedirs (path [, mode])
Function: To create a recursive directory tree, which can be a relative or absolute path, the default mode is also 0777.
If a subdirectory creation fails or already exists, a OSError exception is thrown, and error 183 on Windows is an exception that already exists for the directory. If path has only one level, the same as mkdir. For example:
Attached: (http://blog.csdn.net/ziyuzhao123/article/details/8811384)
Create a Directory
In Python, you can use the Os.mkdir () function to create a directory ( create a first-level directory).
Its prototype is as follows:
Os.mkdir (PATH)
Its argument path is the path to which the directory is to be created.
For example, to create a hello directory under the D drive
>>> Import OS
>>> os.mkdir (' D:\hello ')
You can use the Os.makedirs () function to create a multilevel directory.
Its prototype is as follows:
Os.makedirs (PATH)
Its argument path is the path to which the directory is to be created.
If you create a books directory under the D drive, create the book directory under the Books directory
>>> Import OS
>>>os.makedirs (' D:\\books\\book ')
Delete Directory
In Python, you can use the Os.rmdir () function to delete a directory.
Its prototype is as follows:
Os.rmdir (PATH)
The path to the directory to delete is its parameter.
For example, delete the Hmm directory under the D disk.
>>> Import OS
>>> os.rmdir (' d:\hmm ')
Delete a multilevel directory
In Python, you can use the Os.removedirs () function to delete a multilevel directory.
Its prototype is as follows:
Os.removdirs (PATH)
The path of the multi-level directory to be deleted is its argument path.
>>> Import OS
>>> os.removedirs (' D:\\books\\book ')
#注意: The directory to be deleted must be an empty directory,
deleting files
In Python, you can use the Os.remove () function to delete a file (note that it must be a file).
Its prototype is as follows:
Os.remov (PATH)
The path to the file whose parameter path is to be deleted.
If you delete the Book.txt file in the book directory under the books directory under D disk
>>> Import OS
>>>os.remove (' D:\\books\\book\\book.txt ')
Traverse Directory
In Python, you can use the Os.walk () function to traverse the directory.
Its prototype is as follows:
Os.walk (PATH)
Its parameter path is the directory to traverse, traverse Path, return an object, each of his parts is a ternary group (' Directory x ', [Directory x under directory list], directory x below the file).
Such as:
>>> a=os.walk (' D:\\books ')
>>> def fun ():
For I in A:
Print I
>>> Fun ()
(' D:\\books ', [' book '], [' Aa.txt '])
(' D:\\books\\book ', [], [])
Determine if the directory
In Python, you can use the Os.path.isdir () function to determine whether a path is a directory.
Its function prototype is as follows:
Os.path.isdir (PATH)
Its argument path is the path to be judged. Returns true if yes, otherwise false.
Determine if the file is
In Python, you can use the Os.path.isfile () function to determine whether a path is a file. Its function prototype is shown below.
Os.path.isfile (PATH)
Its argument path is the path to be judged. Returns true if yes, otherwise false.
Python django mkdir and makedirs usage