Os.mkdir and Os.makedirs for OS path-related functions in Python

Source: Internet
Author: User

Portal: http://blog.csdn.net/shennongzhaizhu/article/details/51455063

In Python, you can use the Os.mkdir () function to create a directory ( create a first-level directory).

Os.mkdir (PATH)

For example, to create a hello directory under the D drive

>>> Import OS

>>> os.mkdir (' D:\hello ')

Its prototype is as follows:

Its argument path is the path to the directory to be created ( create a multilevel directory )

>>> Import OS

>>>os.makedirs (' D:\\books\\book ')

In Python, you can use the Os.rmdir () function to delete a directory.

Os.rmdir (PATH)

For example, delete the Hmm directory under the D disk.

>>> Import OS

>>> os.rmdir (' d:\hmm ')

In Python, you can use the Os.removedirs () function to delete a multilevel directory.

Os.removdirs (PATH)

>>> Import OS

>>> os.removedirs (' D:\\books\\book ')

deleting files

Its prototype is as follows:

The path to the file whose parameter path is to be deleted.

>>> Import OS

>>>os.remove (' D:\\books\\book\\book.txt ')

In Python, you can use the Os.walk () function to traverse the directory.

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).

 

>>> a=os.walk (' d:\\books ')
>>> def  Fun ():
                for I in a:
                          print I
>>> fun ()
(' D:\\books ', [' book '], [' Aa.txt '])
(' D:\\books\\book ', [], [])

In Python, you can use the Os.path.isdir () function to determine whether a path is a directory.

Os.path.isdir (PATH)

Its argument path is the path to be judged. Returns true if yes, otherwise false.

In Python, you can use the Os.path.isfile () function to determine whether a path is a file. Its function prototype is shown below.

Its argument path is the path to be judged. Returns true if yes, otherwise false.

Os.mkdir and Os.makedirs for OS path-related functions in Python

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.