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 implement recursive traversal of folder search files by Python

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

Share

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

This article mainly explains "Python how to achieve recursive traversal folder search files," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python how to recursively traverse folder search files"!

Development Background:

There are a lot of e-books in the E disk of the computer. I used to be interested in downloading a lot of e-books. Some read them, some didn't read them. These e-books are not in one place, so I am going to write a script to search out this e-book and sort it out.

Program design ideas:

Define a root directory baseDir for search, a folder list notSearchFolderArr for not search, a file type list searchTypeArr for search,

Determine that the root directory baseDir is valid and does not exist in the notSearhFolderArr array,

Get all the files and folders under the folder,

Traversing, determining if the child element is a file, determining if the file type exists in searchTypeArr, and if so, returning the path.

Determine that the child element is a folder and does not belong to the notSearhFolderArr array, and execute the first step to perform recursive search.

Code:

#Search folder import osimport ioimport syssys.stdout = io.TextIOWrapper according to configured file (sys.stdout.buffer,encoding ='utf8 ')#Main function baseDir = "E:\\Pang\\for_search" #Searched root directory notSearchFolderArr =<$'node_modules']#Not searched directories searchFileTypeArr =<$'. pdf','. PDF']#Searched file type def searhMain(): allResearr = searchFolder(baseDir) print ('\n'. join(allResArr))#Search for a file directory Pass in a file directory path def searchFolder (folderPath): folderName = os.path.split (folderPath)[-1] searFilePathArr = [] if os.path.exists (folderPath) and (folderName not in notSearchFolderArr): fileArr = os.listdir (folderPath) for item in fileArr: currentPath = folderPath+'\\'+item (fileName,fileType) = os.path.splitext(item) if os.path.isfile (currentPath) and (fileType in searchFileTypeArr): searFilePathArr.append (currentPath) if os.path.isdir (currentPath) and (item not in notSearchFolderArr): innerFileArr = searchFolder(currentPath) searFilePathArr.extend(innerFileArr) return searFilePathArrsearhMain()

Main modules and APIs used:

module os: module for operating files

Main APIs:

os.path.split : Split path os.path.exists: Does the path exist os.listdir: If the path is a folder os.path. splittext: Split file extensions in the path from other os.path.isfile: If the path is a file append: Append an element to an array extend: Append an array to an array

Run Results:

The program returns a list of all pdf file paths under the root directory

This script can be modified slightly to query multiple file types, classify the files, obtain the file path to be queried, and also backup the files in batches to a folder, or directly package and compress the search results. Now you can play whatever you want.

At this point, I believe that everyone has a deeper understanding of "Python how to achieve recursive traversal of folder search files," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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