How can I find all the files in a directory having the extension .txt
in python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Here's more versions of the same that produce slightly different results:
glob.iglob()
glob.glob1()
fnmatch.filter()
Use glob.
A copy-pastable solution similar to the one of ghostdog:
In case the folder contains a lot of files or memory is an constraint, consider using generators:
Option A: Iterate
Option B: Get all
Use fnmatch: https://docs.python.org/2/library/fnmatch.html
Python v3.5+
Fast method using os.scandir in a recursive function. Searches for all files with a specified extension in folder and sub-folders.