Article | carefree huan \

Source: Python Technology “ID: pythonall”

There are two ways Python can traverse a folder or file:


import os
for root,dirs,files in os.walk("/Users/cxhuan/Downloads/globtest/hello") :for dir in dirs:
        print(os.path.join(root, dir))
    for file in files:
        print(os.path.join(root, file))
Copy the code

The result of the above code is as follows:

/Users/cxhuan/Downloads/globtest/hello/world
/Users/cxhuan/Downloads/globtest/hello/.DS_Store
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world3.txt
/Users/cxhuan/Downloads/globtest/hello/world/world2.txt
Copy the code

The above program reads all the paths root, directory name dirs, and file name files from OS. walk, i.e., three file arrays, through a foreach loop. The join method joins its path to a directory name or file name to form a complete directory.

The other is to use recursive thinking, written in the following form:


import os
files = list()
def dirAll(pathname):
    if os.path.exists(pathname):
        filelist = os.listdir(pathname)
        for f in filelist:
            f = os.path.join(pathname, f)
            if os.path.isdir(f):
                dirAll(f)
            else:
                dirname = os.path.dirname(f)
                baseName = os.path.basename(f)
                if dirname.endswith(os.sep):
                    files.append(dirname+baseName)
                else:
                    files.append(dirname+os.sep+baseName)


dirAll("/Users/cxhuan/Downloads/globtest/hello")
for f in files:
    print(f)
Copy the code

Run the code above and you get the same result.

These two methods are ok, but it is more troublesome to write, especially the second one, if you are not careful, you may also write bugs.

Today we’ll look at the third method — using the Glob module to traverse the file.

Introduction to the

Glob is a python module that operates on files and is known for its simplicity and utility. Because the function of this module is relatively simple, it is also easy to get started and use. It is mainly used to find file paths that conform to certain rules. To use this module to find files, just use *,? And [] :

* : matching0One or more characters; ? : Matches a single character. [] : matches characters in the specified range, for example: [09 -] Matches the number.Copy the code

Glob. Glob method

The glob. Glob method returns a list of all matched file paths. It has only one parameter, pathName, that defines the rules for matching file paths, which can be absolute or relative.

use*matching

We can match zero or more characters with *.

Subdirectories or files in the output directory:


for p1 in glob.glob('/Users/cxhuan/Downloads/globtest/*') :print(p1)
Copy the code

Running the code above will output the only directory in the Globtest folder:


/Users/cxhuan/Downloads/globtest/hello
Copy the code

We can also create hierarchies to traverse files or folders:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/*/*') :print(p)
Copy the code

The code above iterates through the Globtest folder and its subfolders, printing out all the files or folder paths:

/Users/cxhuan/Downloads/globtest/hello/world
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
Copy the code

We can also filter files or folders:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*3.txt') :print(p)
Copy the code

The above code value matches the TXT file whose name ends with ‘3’ in the hello directory, and the result is as follows:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt
Copy the code

use?matching

We can use question marks (?). Matches any single character.


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello? .txt') :print(p)
Copy the code

The code above prints a TXT file starting with ‘hello’ in the hello directory. The output is as follows:


/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
Copy the code

use[]matching

We can use [] to match a range:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*[0-2].*') :print(p)
Copy the code

We want to get a file in the hello directory where the number at the end of the file name ranges from 0 to 2. Run the code above and get the output:

/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
Copy the code

Glob. Iglob method

Python’s glob method iterates over all the files in a folder and returns a list. The IGLOb method takes only one match path at a time. Here’s a simple example to illustrate the difference:

p = glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello? . * ')
print(p)

print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --')

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello? . * ')
print(p)
Copy the code

Running the above code returns:


['/Users/cxhuan/Downloads/globtest/hello/hello3.txt'.'/Users/cxhuan/Downloads/globtest/hello/hello2.txt'.'/Users/cxhuan/Downloads/globtest/hello/hello1.txt']
----------------------
<generator object _iglob at 0x1040d8ac0>
Copy the code

It is easy to see the difference between the above result, which returns a list, and the latter, which returns an iterable.

Let’s do something about the iterable:

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello? . * ')
print(p.__next__())
print(p.__next__())
Copy the code

The running results are as follows:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
Copy the code

We can see that we can get one element at a time for the iterable. The advantage of this is to save memory. If there are a large number of folders or files in a path, we can use this iterator to retrieve memory not all at once, but slowly.

conclusion

Although the module shared today is simple, it is enough for us to traverse a file or directory, and the method is easy to understand, it is worth using frequently. If you find the modules shared today useful, click “Watching” to support!

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the text, reply: 210318