Python walk function

Source: Internet
Author: User

Os.walk method

1 Import OS 2  for  in Os.walk (R'C:\Users\jack\Desktop\test\3_ language edition seven-year book \1_ one unit '):  3     print(i[0])

The Os.walk method returns a ternary tupple (Dirpath, Dirnames, filenames),

The first one is the starting path,

The second is a folder under the starting path,

The third one is the file under the starting path.

Dirpath is a string that represents the path to the directory,

Dirnames is a list that contains the names of all subdirectories under Dirpath,

Filenames is a list that contains the name of a non-directory file.

These names do not contain path information and need to use Os.path.join (Dirpath, name) if you need to get the full path.

1. Print i[0], list the full path of each folder in the walk path.

2. Print I[1], list all the folders under the walk path, starting with the first folder, layers in depth, if there are no folders, the display is empty [].

3. Print I[2], return to the walk path, all the file names (not including the folder name), starting from the first folder, if there is no file in the folder directory, only the folder, then return empty [].

You can also directly write 3 variable names to receive the Walk return value:

1 Import OS 2  for  in Os.walk (R'C:\Users\jack\Desktop\test\3_ language edition seven-year book \1_ one unit '):  3     print(dirnames)

Python walk function

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.