The use and examples of Python Os.walk

Source: Internet
Author: User

Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false)

Can get a ternary tupple (Dirpath, Dirnames, filenames),

The first is the starting path, the second is the folder under the starting path, and the third 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 you need to use Os.path.join (Dirpath, name) if you need to get the full path.

Auto-complete recursive enumeration with a for loop

For example:

The F:\AAA directory is such a file directory structure

F:\aaa

|--------1.txt

|--------2.txt

|--------3.txt

|--------4

|-------5.txt

|-------6.txt

|-------7.txt

Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false) can get a ternary tupple (Dirpath, Dirnames, filenames), the first one for the starting path, The second is the folder under the starting path, and the third one is the file under the starting path. Dirpath is a string that represents the path to the directory, and 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 if you need to get the full path, you need to use Os.path.join (Dirpath, name). Auto-complete recursive enumeration via for loop for example: F:\aaa directory is such a file directory structure f:\aaa|--------1.txt| --------2.txt|--------3.txt|--------4         |-------5.txt         |-------6.txt         |-------7.txt
os.walk (Top, Topdown=true, Onerror=none, Followlinks=false) can get a ternary tupple ( Dirpath, Dirnames, filenames), the first is the starting path, the second is the folder under the starting path, and the third is the file under the starting path. Dirpath is a string that represents the path to the directory, and 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 if you need to get the full path, you need to use Os.path.join (Dirpath, name). Auto-complete recursive enumeration via for loop for example: F:\aaa directory is such a file directory structure f:\aaa|--------1.txt| --------2.txt|--------3.txt|--------4 |-------5.txt |-------6.txt |-------7.txt 
Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false) can get a ternary tupple (Dirpath, Dirnames, filenames), the first one for the starting path, The second is the folder under the starting path, and the third one is the file under the starting path. Dirpath is a string that represents the path to the directory, and 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 if you need to get the full path, you need to use Os.path.join (Dirpath, name). Auto-complete recursive enumeration via for loop for example: F:\aaa directory is such a file directory structure f:\aaa|--------1.txt| --------2.txt|--------3.txt|--------4         |-------5.txt         |-------6.txt         |-------7.txt

  

  

  

The use and examples of Python Os.walk

Related Article

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.