Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the os.walk () function in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to understand the os.walk () function in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Os.walk () is a function that traverses the number of directories and accesses the specified directory with a depth-first policy (depth-first).

It returns (root,dirs, files)

Root represents the directory path currently traversed, string type

Dirs represents all subdirectory names under the root path, list type, and each element in the list is of string type, representing the subdirectory name.

Files represents all the subfile names under the root path and returns the list type, and each element in the list is of string type, representing the subfile name.

Add to my current directory as follows.

You can first print how it is traversed:

Import osfrom os.path import joinhome_path = "/ home" for (root, dirs, files) in os.walk (home_path): print (root) print (dirs) print (files) print

The output is as follows:

/ home

['root',' zhang', 'li']

['test.txt',' hai.mp4']

=

/ home/root

[]

['1.txthammer,' 2.txtforth, '3.txt']

=

/ hoome/zhang

[]

['zhang_1.mp4',' zhang_2.mp4', 'zhang_3.mp4']

=

/ home/li

[]

[]

=

There are three lines.

Line 1 represents the directory currently traversed, which we call the root directory

Line 2 represents a list of subdirectories under the root directory, which we call dirs

Line 3 represents a list of subfiles in the root directory, which we call files

The empty list above means that there are no subdirectories or subfiles under the current traversing root directory.

In addition, if I want to traverse the absolute paths of all directories and files under the home directory, I can simply use the os.path.join () method to concatenate the subdirectory or subfile name and the root directory. The code is as follows:

Import osfrom os.path import joinhome_path = "/ home" for (root, dirs, files) in os.walk (home_path): for dir in dirs: print (join (root, dir)) for file in files: print (join (root, file))

Output:

/ home

/ home/root

/ home/zhang

/ home/li

/ home/test.txt

/ home/hai.mp4

/ home/root/1.txt

/ home/root/2.txt

/ home/root/3.txt

/ home/zhang/zhang_1.mp4

/ home/zhang/zhang_2.mp4

/ home/zhang/zhang_3.mp4

That's all for "how to understand the os.walk () function in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report