


How to get all files in a specified directory using Python code set pathlib application
(1) The following code, by default, recursively obtains all files in the specified directory root_dir. When the recursive parameter is specified as False, only all files in the root_dir directory are obtained, and no recursive search is performed. If the suffix_tuple parameter is specified, then You can obtain the specified suffix file in the root_dir directory
from pathlib import Path def get_all_files(root_dir,recursive=True,suffix_tuple=()): all_files=[] if Path(root_dir).exists(): if Path(root_dir).is_dir(): if recursive: for elem in Path(root_dir).glob("**/*"): if Path(elem).is_file(): suffix=Path(elem).suffix if not suffix_tuple: all_files.append(elem) else: if suffix in suffix_tuple: all_files.append(elem) else: for elem in Path(root_dir).iterdir(): if Path(elem).is_file(): suffix=Path(elem).suffix if not suffix_tuple: all_files.append(elem) else: if suffix in suffix_tuple: all_files.append(elem) else: all_files.append(root_dir) return all_files
(2) The specific usage method is as follows, that is, the test code, the specific directory path is specified as the directory where it exists
if __name__=="__main__": path="D:/gitee/oepkgs/mugen/testcases/cli-test/acl/oe_test_acl_defaulr_rule.sh" for elem in get_all_files(path): print(elem) print("-------------------------------------------------") path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl" for elem in get_all_files(path): print(elem) print("-------------------------------------------------") path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl" for elem in get_all_files(path,False): print(elem) print("-------------------------------------------------") path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl" for elem in get_all_files(path, True,(".sh",)): print(elem) print("-------------------------------------------------") path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl" for elem in get_all_files(path, True, (".json",)): print(elem)
The execution results are as follows, first When the path is specified as a file, the file is directly returned as the query result. The last one specifies the .json suffix. Because there is no json file on the debugging machine, the print is empty
The above is the detailed content of How to get all files in a specified directory using Python code set pathlib application. For more information, please follow other related articles on the PHP Chinese website!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

NumPyallowsforvariousoperationsonarrays:1)Basicarithmeticlikeaddition,subtraction,multiplication,anddivision;2)Advancedoperationssuchasmatrixmultiplication;3)Element-wiseoperationswithoutexplicitloops;4)Arrayindexingandslicingfordatamanipulation;5)Ag

ArraysinPython,particularlythroughNumPyandPandas,areessentialfordataanalysis,offeringspeedandefficiency.1)NumPyarraysenableefficienthandlingoflargedatasetsandcomplexoperationslikemovingaverages.2)PandasextendsNumPy'scapabilitieswithDataFramesforstruc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
