Python Learning Os.walk ()

Source: Internet
Author: User

Os.walk (Top,topdown = True,onerror = None,followlinks = False)
Parameters
    • Top -Every folder in the root directory (including itself), resulting in 3-tuple (Dirpath, dirnames, filenames) "Folder path, folder name, file name".

    • Topdown --optional, true or unspecified, a 3-tuple of a directory will be generated first (directory top-down) than the 3-tuple of any of its subfolders. If Topdown is False, a 3-tuple of a directory will be generated after the 3-tuple of any of its subfolders (directory bottom-up).

    • onerror --optional, is a function; It is called when there is a parameter, an OSError instance. After reporting this error, continue to walk, or throw exception to terminate walk.

    • followlinks --set to True to access the directory through a soft link.

return value

The method has no return value.

Instance

#-*-Coding:utf-8-*-#import osimport os.pathfor root, dirs, files in Os.walk ("D:\Leo\cew", Topdown=false): for    name In Files: print (        name)        print ('-' *10) for    name in dirs: print (        name)        print (' # ' *10)

Execution results

2.txt
----------
ew
##########
New text document. txt
----------
1
##########
2
##########

Querying with the tree command

Tree d:\leo\cewd:\leo\cew├─ 1 └─ 2     └─ew

Python Learning Os.walk ()

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.