Python's operation of the file is also convenient, only need to include the OS module in, using the relevant functions to achieve the creation of the directory.
It mainly involves three functions
1.os.path.exists (path) to determine if a directory exists
2.os.makedirs (PATH) multi- layer Create directory
3.Os.mkdir (path) Create directory
def mkdir (path): # Introduce module import OS # Remove first space Path=path.strip () # remove tail \ Symbol path=path.rstrip ("\ \ # to determine if the path exists # exists True # does not exist False isexists=os.path.exists (path) # judgment result if not Isexists: # Create directory if not present print path+ ' Create successful ' # Create directory operation function os.makedirs (path) return True else: # If the directory exists then do not create and prompt the directory already exists print path+ ' directory already exists ' return False # Defines the directory to create Mkpath= "f:\\wfpdm\\20150727_ 1010\\ "# Call function mkdir (Mkpath)
Description
In the above demo function, I did not use the Os.mkdir (path) function, but instead used multiple layers to create the directory function os.makedirs (path). The biggest difference between the two functions is that os.mkdir (path) is not created when the parent directory does not exist, and Os.makedirs creates the parent directory .
For example: In the example I want to create the directory Web is located in the D-disk QTTC directory, but I do not have QTTC under the D disk parent directory, if you use the Os.mkdir (path) function will prompt me the target path does not exist, but use Os.makedirs (path) will automatically help me create the parent directory QTTC, create the subdirectory Web under the QTTC directory.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python's Create Directory folder