tags:

views:

220

answers:

7

How can I find all files in directory with the extension .txt in python?

Thanks.

UPDATE: Thanks everyone, wide variety of examples for the next person that searches for this.

+4  A: 

Use glob.

>>> import glob
>>> glob.glob('./*.txt')
['./outline.txt', './pip-log.txt', './test.txt', './testingvim.txt']
Muhammad Alkarouri
+4  A: 

Something like that should do the job

for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith('.txt')
            print file
Adam Byrtek
+12  A: 

you can use glob

import glob
import os
os.chdir("/mydir")
for files in glob.glob("*.txt"):
    print files

or simple os.listdir

import os
os.chdir("/mydir")
for files in os.listdir("."):
    if files.endswith(".txt"):
        print files

or if you want to traverse directory

import os
for r,d,f in os.walk("/mydir"):
    for files in f:
        if files.endswith(".txt"):
             print os.path.join(r,files)
ghostdog74
Using solution #2, How would you create a file or list with that info?
user428862: its not to create files, but to list files in a directory
usertest
don't understand. elaborate more.
ghostdog74
Thanks ghostdog, just tested two, works well.
usertest
`glob.glob(..)` is just `list(glob.iglob(..))`. `os.chdir()` is unnecessary http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python/3971553#3971553
J.F. Sebastian
@ghostdog74: In my opinion it would more appropriate to write `for file in f` than for `for files in f` since what is in the variable is a single filename. Even better would be to change the `f` to `files` and then the for loops could become `for file in files`.
martineau
+2  A: 

glob.glob()

Ignacio Vazquez-Abrams
+3  A: 

Something like this will work:

>>> import os
>>> path = '/usr/share/cups/charmaps'
>>> text_files = [f for f in os.listdir(path) if f.endswith('.txt')]
>>> text_files
['euc-cn.txt', 'euc-jp.txt', 'euc-kr.txt', 'euc-tw.txt', ... 'windows-950.txt']
Seth
+2  A: 

I like os.walk():

import os, os.path

for root, dirs, files in os.walk(dir):
    for f in files:
        fullpath = os.path.join(root, f)
        if os.path.splitext(fullpath)[1] == '.txt':
            print fullpath

Or with generators:

import os, os.path

fileiter = (os.path.join(root, f)
    for root, _, files in os.walk(dir)
    for f in files)
txtfileiter = (f for f in fileiter if os.path.splitext(f)[1] == '.txt')
for txt in txtfileiter:
    print txt
hughdbrown
+1  A: 

Here's more versions of the same that produce slightly different results:

glob.iglob()

import glob
for f in glob.iglob("/mydir/*/*.txt"): # generator, search immediate subdirectories 
    print f

glob.glob1()

print glob.glob1("/mydir", "*.tx?")  # literal_directory, basename_pattern

fnmatch.filter()

import fnmatch, os
print fnmatch.filter(os.listdir("/mydir"), "*.tx?") # include dot-files
J.F. Sebastian
For the curious, `glob1()` is a helper function in the `glob` module which isn't listed in the Python documentation. There's some inline comments describing what it does in the source file, see `.../Lib/glob.py`.
martineau
@martineau: `glob.glob1()` is not public but it is available on Python 2.4-2.7;3.0-3.2; pypy; jython http://github.com/zed/test_glob1
J.F. Sebastian
@J.F. Sebastian: Thanks, that's good additional information to have when deciding whether to use a undocumented private function in a module. ;-) Here's a little more. The Python 2.7 version is only 12 lines long and looks like it could easily be extracted from the `glob` module.
martineau