In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.