在Python中查找扩展名为.txt的目录中的所有文件

use*_*est 1043 python file-io

如何.txt在python中找到扩展名目录中的所有文件?

gho*_*g74 2128

你可以使用glob:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)
Run Code Online (Sandbox Code Playgroud)

或者干脆os.listdir:

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        print(os.path.join("/mydir", file))
Run Code Online (Sandbox Code Playgroud)

或者如果要遍历目录,请使用os.walk:

import os
for root, dirs, files in os.walk("/mydir"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))
Run Code Online (Sandbox Code Playgroud)

  • @ ghostdog74:在我看来,为ff中的文件编写`比用于f`中的文件更合适,因为变量中的文件是单个文件名.更好的是将`f`更改为`files`,然后for循环可以变为`for files in files`. (67认同)
  • @computermacgyver:不,`file`不是保留字,只是预定义函数的名称,所以很有可能在自己的代码中将它用作变量名.虽然通常人们应该避免像这样的冲突,但`file`是一个特例,因为几乎没有必要使用它,所以通常认为指南是例外.如果您不想这样做,PEP8建议在这些名称后附加一个下划线,即`file_`,您必须同意它仍然具有可读性. (43认同)
  • 如果f.endswith('.txt')],则#2的更多Pythonic方式可以是[f for os.listdir('/ mydir')中的文件**:** (37认同)
  • `chdir` 不仅是不必要的,而且是非常不受欢迎的,因为函数调用不应该不可逆地修改当前工作目录。这可能会导致很多下游错误。 (22认同)
  • 使用解决方案#2,您将如何使用该信息创建文件或列表? (9认同)
  • 谢谢,马蒂诺,你说得对.我跳得太快得出结论. (8认同)
  • @martineau`file`是一个保留字,不能用作变量.但是,可能更清楚地做`for f in files`并切换其余的代码,这样f就是一个文件,文件就是列表 (3认同)
  • 如果os.path.isfile(os.path.join("/ mydir",f))和f.endswith(".txt")我找到`text_files = [f for os.listdir("/ mydir")中的f ]`比第二个选项更快地工作.. (3认同)
  • @Merlin,在 for 循环之前,初始化一个空列表,比如 fileList = [ ],然后用 fileList.append(os.path.join(root, file)) 替换循环的最后一条语句。 (2认同)

Muh*_*uri 218

使用glob.

>>> import glob
>>> glob.glob('./*.txt')
['./outline.txt', './pip-log.txt', './test.txt', './testingvim.txt']
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果你的python低于3.5,`glob`无法递归地找到文件**.[更多信息](http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python) (28认同)
  • 这只查找当前顶级目录中的文件,而不是整个目录中的文件。 (3认同)

Ada*_*tek 136

这样的事情应该可以胜任

for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith('.txt'):
            print file
Run Code Online (Sandbox Code Playgroud)

  • +1用于命名变量`root,dirs,files`而不是`r,d,f`.更具可读性. (62认同)
  • 请注意,这是区分大小写的(不会匹配.TXT或.Txt),所以如果file.lower().endswith('.txt')你可能会想要这样做: (20认同)
  • 您的答案涉及子目录。 (3认同)

Set*_*eth 105

这样的东西会起作用:

>>> 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']
Run Code Online (Sandbox Code Playgroud)

  • 你可以在`text_files`的每个元素上使用[`os.path.join`](https://docs.python.org/2/library/os.path.html#os.path.join).如果f.endswith('.txt')]`,它可能类似于`text_files = [os.path.join(path,f),用于os.listdir(path)中的f. (4认同)

小智 32

import os

path = 'mypath/path' 
files = os.listdir(path)

files_txt = [i for i in files if i.endswith('.txt')]
Run Code Online (Sandbox Code Playgroud)


MSe*_*ert 30

你可以简单地使用pathlibs 1:glob

import pathlib

list(pathlib.Path('your_directory').glob('*.txt'))
Run Code Online (Sandbox Code Playgroud)

或循环:

for txt_file in pathlib.Path('your_directory').glob('*.txt'):
    # do something with "txt_file"
Run Code Online (Sandbox Code Playgroud)

如果你想要递归,你可以使用 .glob('**/*.txt)


1pathlib模块包含在python 3.4的标准库中.但是你甚至可以在较旧的Python版本(即使用condapip)上安装该模块的后端端口:pathlibpathlib2.

  • 请注意,如果要递归查找项目,也可以使用`rglob`。例如`.rglob('*。txt')` (2认同)
  • @gota 获取所有文件,然后排除 txt 文件 `set(pathlib.Path('.').iterdir()) - set(pathlib.Path().glob("*.txt"))` (2认同)

hug*_*own 27

我喜欢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
Run Code Online (Sandbox Code Playgroud)

或者使用发电机:

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
Run Code Online (Sandbox Code Playgroud)


jfs*_*jfs 25

这里的更多版本会产生稍微不同的结果:

glob.iglob()

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

glob.glob1()

print glob.glob1("/mydir", "*.tx?")  # literal_directory, basename_pattern
Run Code Online (Sandbox Code Playgroud)

fnmatch.filter()

import fnmatch, os
print fnmatch.filter(os.listdir("/mydir"), "*.tx?") # include dot-files
Run Code Online (Sandbox Code Playgroud)

  • 对于好奇的,`glob1()`是`glob`模块中的一个辅助函数,它没有在Python文档中列出.有一些内联注释描述它在源文件中的作用,参见`.../Lib/glob.py`. (3认同)

Anu*_*har 19

path.py是另一种选择:https://github.com/jaraco/path.py

from path import path
p = path('/path/to/the/directory')
for f in p.files(pattern='*.txt'):
    print f
Run Code Online (Sandbox Code Playgroud)

  • 雅还有路径库。您可以执行以下操作:``list(p.glob('**/*.py'))`` (2认同)

Xxx*_*xxo 11

Python有所有工具可以做到这一点:

import os

the_dir = 'the_dir_that_want_to_search_in'
all_txt_files = filter(lambda x: x.endswith('.txt'), os.listdir(the_dir))
Run Code Online (Sandbox Code Playgroud)


ewa*_*lel 10

将'dataPath'文件夹中的所有'.txt'文件名称作为Pythonic方式的列表

from os import listdir
from os.path import isfile, join
path = "/dataPath/"
onlyTxtFiles = [f for f in listdir(path) if isfile(join(path, f)) and  f.endswith(".txt")]
print onlyTxtFiles
Run Code Online (Sandbox Code Playgroud)


Dou*_*ugR 10

Python v3.5 +

在递归函数中使用os.scandir的快速方法。在文件夹和子文件夹中搜索具有指定扩展名的所有文件。

import os

def findFilesInFolder(path, pathList, extension, subFolders = True):
    """  Recursive function to find all files of an extension type in a folder (and optionally in all subfolders too)

    path:        Base directory to find files
    pathList:    A list that stores all paths
    extension:   File extension to find
    subFolders:  Bool.  If True, find files in all subfolders under path. If False, only searches files in the specified folder
    """

    try:   # Trapping a OSError:  File permissions problem I believe
        for entry in os.scandir(path):
            if entry.is_file() and entry.path.endswith(extension):
                pathList.append(entry.path)
            elif entry.is_dir() and subFolders:   # if its a directory, then repeat process as a nested function
                pathList = findFilesInFolder(entry.path, pathList, extension, subFolders)
    except OSError:
        print('Cannot access ' + path +'. Probably a permissions error')

    return pathList

dir_name = r'J:\myDirectory'
extension = ".txt"

pathList = []
pathList = findFilesInFolder(dir_name, pathList, extension, True)
Run Code Online (Sandbox Code Playgroud)

2019年4月更新

如果要搜索包含10,000s个文件的目录,则附加到列表的效率将降低。“屈服”结果是一个更好的解决方案。我还提供了一个将输出转换为Pandas Dataframe的功能。

import os

def findFilesInFolder(path, pathList, extension, subFolders = True):
    """  Recursive function to find all files of an extension type in a folder (and optionally in all subfolders too)

    path:        Base directory to find files
    pathList:    A list that stores all paths
    extension:   File extension to find
    subFolders:  Bool.  If True, find files in all subfolders under path. If False, only searches files in the specified folder
    """

    try:   # Trapping a OSError:  File permissions problem I believe
        for entry in os.scandir(path):
            if entry.is_file() and entry.path.endswith(extension):
                pathList.append(entry.path)
            elif entry.is_dir() and subFolders:   # if its a directory, then repeat process as a nested function
                pathList = findFilesInFolder(entry.path, pathList, extension, subFolders)
    except OSError:
        print('Cannot access ' + path +'. Probably a permissions error')

    return pathList

dir_name = r'J:\myDirectory'
extension = ".txt"

pathList = []
pathList = findFilesInFolder(dir_name, pathList, extension, True)
Run Code Online (Sandbox Code Playgroud)


mrg*_*oom 8

import os
import sys 

if len(sys.argv)==2:
    print('no params')
    sys.exit(1)

dir = sys.argv[1]
mask= sys.argv[2]

files = os.listdir(dir); 

res = filter(lambda x: x.endswith(mask), files); 

print res
Run Code Online (Sandbox Code Playgroud)


use*_*036 8

我做了一个测试(Python 3.6.4,W7x64),看看哪个解决方案对于一个文件夹,没有子目录来说是最快的,以获得具有特定扩展名的文件的完整文件路径列表.

为了缩短它,这项任务os.listdir()是最快的,并且速度是次佳的1.7 os.walk()倍:(有休息!),2.7 倍速,比pathlib快3 倍,比os.scandir()快3.3倍glob.
请记住,当您需要递归结果时,这些结果会发生变化.如果您复制/粘贴下面的一个方法,请添加.lower(),否则在搜索.ext时找不到.EXT.

import os
import pathlib
import timeit
import glob

def a():
    path = pathlib.Path().cwd()
    list_sqlite_files = [str(f) for f in path.glob("*.sqlite")]

def b(): 
    path = os.getcwd()
    list_sqlite_files = [f.path for f in os.scandir(path) if os.path.splitext(f)[1] == ".sqlite"]

def c():
    path = os.getcwd()
    list_sqlite_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".sqlite")]

def d():
    path = os.getcwd()
    os.chdir(path)
    list_sqlite_files = [os.path.join(path, f) for f in glob.glob("*.sqlite")]

def e():
    path = os.getcwd()
    list_sqlite_files = [os.path.join(path, f) for f in glob.glob1(str(path), "*.sqlite")]

def f():
    path = os.getcwd()
    list_sqlite_files = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(".sqlite"):
                list_sqlite_files.append( os.path.join(root, file) )
        break



print(timeit.timeit(a, number=1000))
print(timeit.timeit(b, number=1000))
print(timeit.timeit(c, number=1000))
print(timeit.timeit(d, number=1000))
print(timeit.timeit(e, number=1000))
print(timeit.timeit(f, number=1000))
Run Code Online (Sandbox Code Playgroud)

结果:

# Python 3.6.4
0.431
0.515
0.161
0.548
0.537
0.274
Run Code Online (Sandbox Code Playgroud)


小智 6

试试这个,这将递归地找到所有文件:

import glob, os
os.chdir("H:\\wallpaper")# use whatever you directory 

#double\\ no single \

for file in glob.glob("**/*.psd", recursive = True):#your format
    print(file)
Run Code Online (Sandbox Code Playgroud)

  • 以前的帖子中已经提供了此答案 (3认同)
  • 好吧,您可以使用 os 库来加入路径,例如 `filepath = os.path.join('wallpaper')` ,然后将其用作 `glob.glob(filepath+"**/*.psd", recursive) = True)`,这会产生相同的结果。 (2认同)

Kam*_*kov 6

要从同一目录中名为“data”的文件夹中获取“.txt”文件名数组,我通常使用以下简单的代码行:

import os
fileNames = [fileName for fileName in os.listdir("data") if fileName.endswith(".txt")]
Run Code Online (Sandbox Code Playgroud)


pra*_*890 5

这段代码让我的生活更简单.

import os
fnames = ([file for root, dirs, files in os.walk(dir)
    for file in files
    if file.endswith('.txt') #or file.endswith('.png') or file.endswith('.pdf')
    ])
for fname in fnames: print(fname)
Run Code Online (Sandbox Code Playgroud)


yuc*_*cer 5

使用fnmatch:https://docs.python.org/2/library/fnmatch.html

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print file
Run Code Online (Sandbox Code Playgroud)


Mar*_*oma 5

类似于 Ghostdog 的可复制粘贴解决方案:

def get_all_filepaths(root_path, ext):
    """
    Search all files which have a given extension within root_path.

    This ignores the case of the extension and searches subdirectories, too.

    Parameters
    ----------
    root_path : str
    ext : str

    Returns
    -------
    list of str

    Examples
    --------
    >>> get_all_filepaths('/run', '.lock')
    ['/run/unattended-upgrades.lock',
     '/run/mlocate.daily.lock',
     '/run/xtables.lock',
     '/run/mysqld/mysqld.sock.lock',
     '/run/postgresql/.s.PGSQL.5432.lock',
     '/run/network/.ifstate.lock',
     '/run/lock/asound.state.lock']
    """
    import os
    all_files = []
    for root, dirs, files in os.walk(root_path):
        for filename in files:
            if filename.lower().endswith(ext):
                all_files.append(os.path.join(root, filename))
    return all_files
Run Code Online (Sandbox Code Playgroud)

您还可以使用它yield来创建生成器,从而避免组装完整列表:

def get_all_filepaths(root_path, ext):
    """
    Search all files which have a given extension within root_path.

    This ignores the case of the extension and searches subdirectories, too.

    Parameters
    ----------
    root_path : str
    ext : str

    Returns
    -------
    list of str

    Examples
    --------
    >>> get_all_filepaths('/run', '.lock')
    ['/run/unattended-upgrades.lock',
     '/run/mlocate.daily.lock',
     '/run/xtables.lock',
     '/run/mysqld/mysqld.sock.lock',
     '/run/postgresql/.s.PGSQL.5432.lock',
     '/run/network/.ifstate.lock',
     '/run/lock/asound.state.lock']
    """
    import os
    all_files = []
    for root, dirs, files in os.walk(root_path):
        for filename in files:
            if filename.lower().endswith(ext):
                all_files.append(os.path.join(root, filename))
    return all_files
Run Code Online (Sandbox Code Playgroud)