Python's Create Directory folder

Source: Internet
Author: User
Tags create directory

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.