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 one with
extend()
You can simply use
pathlib
sglob
1:or in a loop:
If you want it recursive you can use
.glob('**/*.txt)
1The
pathlib
module was included in the standard library in python 3.4. But you can install back-ports of that module even on older Python versions (i.e. usingconda
orpip
):pathlib
andpathlib2
.To get an array of ".txt" file names from a folder called "data" in the same directory I usually use this simple line of code:
Something like that should do the job
Functional solution with sub-directories: