Hello, it’s time for the Python Office Automation series. In everyday work, we often search for a particular file from a pile of files (folders) in different formats. You may be looking for it from memory or using software, but have you ever thought about how to do it in Python?

Scan the contents of a path

Sometimes we want to quickly find the files we need among the hundreds or thousands of files in the current folder. If the folder contains many subfolders, we don’t need to use resources to find them. This is a classic scan layer search.

  1. Based on OS. The scandir ()

specifications

Run the C:\Program Files (x86) command to count the number of folders whose names contain Windows

Step analysis

This is a non-traversal requirement that requires only a scan around the destination folder. The method used is os.scandir(), as follows:

import ospath = … for file in os.scandir(path): print(file.name, file.path, file.is_dir())

The last output of the above code is the name of each content in the given path, the absolute path, and determine whether it is a folder

It is important to note that os.scandir() scans only at the lower level of the path.

import osnum = 0for file in os.scandir(r’C:\Program Files (x86)’): if file.is_dir(): if ‘Windows’ in file.name: Print (file.name) num += 1print(‘ x ‘, num)

  1. Based on the OS. Listdir ()

specifications

Output C:\Program Files (x86) all executable Files (suffix.exe)

Step analysis

Again a non-traversal requirement, os.listdir() is used here, which is simpler than os.scandir() and calls output names directly instead of paths. The suffix name can be determined by slicing up the string, but it inevitably loses flexibility. Use the string method string.endswith() to check whether the name endswith.exe.

import os for file in os.listdir(r’C:\Program Files (x86)’): if file.endswith(‘.exe’): print(file)

3. Traverse folders to search for files

Most of the time, we want to give a general path, and we want to go through all the folders in this path layer by layer to find specific files or files that meet the requirements. Here, we need to traverse the files. There are two main methods:

  1. Based on the OS. Walk ()

Os. walk Generates three parameters: the path of the current folder, including the folder name [list], and the file name [list]. You can complete the walk with the following code:

Import osfor dirPath, dirnames, filenames in os.walk(r’C:\Program Files (x86)’): print(f’ open folder {dirPath}’) if dirnames: print(dirnames) if filenames: print(filenames) print(‘-‘ * 10)

specifications

Traverse C:\Program Files (x86) to find all new Excel Files (suffix.xlsx)

Step analysis

Once you understand how os.walk() works, use endswith() to determine the suffix. Finally, if you need to get the absolute path, you can concatenate the current folder path with the file name, and simply format it with + or string, or you can use the method in the OS module

import osfor dirpath, dirnames, filenames in os.walk(r’C:\Program Files (x86)’): if filenames: for i in filenames: if i.endswith(‘.xlsx’): print(os.path.join(dirpath, i))

  1. Based on non-OS method: Glob

Glob also mentioned the traversal framework repeatedly in previous tweets:

import globfor file in glob.glob(‘**/*’, recursive=True): print(file)

The use of **/* indicates that a wildcard is used to refer to any level under a given path. The recursive parameter allows traversal of the search. Since globs can use “wildcards”, the flexibility is greatly expanded

specifications

Traverse C:\Program Files (x86) to find all new Excel Files (suffix.xlsx)

import globfor file in glob.glob(‘**/*.xlsx’, recursive=True): print(file)

As you can see, it is very simple to add a suffix to the original code to complete the search for a specific type of file. If you need to search for “specific Files”, such as C:\Program Files (x86) to find the file practice.txt, “just change the last * to the specific name”.

import globfor file in glob.glob(‘**/practice.txt’, recursive=True): print(file)

Write at the end

Through the Python automation script making process of this article, we can experience the power of Python office automation again! If you are interested in automated code and data, you can reply to automated retrieval in the background.

Of course, this article is based on a few simple requirements to explain the main ways to use Python to script search files. Then you can combine the previous automation examples or add some additional rules or new features based on your own requirements to create your own software!

For more python, add Q3044236899

Author: Xiao Yu teaches Python