Find all files in a directory with extension .txt in Python
在python中,如何找到扩展名为
您可以使用
1 2 3 4 | import glob, os os.chdir("/mydir") for file in glob.glob("*.txt"): print(file) |
或者简单地说是
1 2 3 4 | import os for file in os.listdir("/mydir"): if file.endswith(".txt"): print(os.path.join("/mydir", file)) |
或者,如果要遍历目录,请使用
1 2 3 4 5 | import os for root, dirs, files in os.walk("/mydir"): for file in files: if file.endswith(".txt"): print(os.path.join(root, file)) |
使用球形。
1 2 3 | >>> import glob >>> glob.glob('./*.txt') ['./outline.txt', './pip-log.txt', './test.txt', './testingvim.txt'] |
像那样的东西应该能完成这项工作
1 2 3 4 | for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.txt'): print file |
类似这样的事情会奏效:
1 2 3 4 5 | >>> 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'] |
1 2 3 4 5 6 | import os path = 'mypath/path' files = os.listdir(path) files_txt = [i for i in files if i.endswith('.txt')] |
我喜欢os.walk():
1 2 3 4 5 6 7 | 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 |
或使用发电机:
1 2 3 4 5 6 7 8 | 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 |
这里有更多版本的相同的,产生稍微不同的结果:
GLUB.IGLUBL()1 2 3 | import glob for f in glob.iglob("/mydir/*/*.txt"): # generator, search immediate subdirectories print f |
GLUB.GROUL1()
1 | print glob.glob1("/mydir","*.tx?") # literal_directory, basename_pattern |
fnmatch.filter()。
1 2 | import fnmatch, os print fnmatch.filter(os.listdir("/mydir"),"*.tx?") # include dot-files |
path.py是另一种选择:https://github.com/jaraco/path.py
1 2 3 4 | from path import path p = path('/path/to/the/directory') for f in p.files(pattern='*.txt'): print f |
您只需使用
1 2 3 | import pathlib list(pathlib.Path('your_directory').glob('*.txt')) |
或者在一个循环中:
1 2 | for txt_file in pathlib.Path('your_directory').glob('*.txt'): # do something with"txt_file" |
如果需要递归,可以使用
1
python有所有的工具来实现这一点:
1 2 3 4 | import os the_dir = 'the_dir_that_want_to_search_in' all_txt_files = filter(lambda x: x.endswith('.txt'), os.listdir(the_dir)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
你可以试试这个代码
1 2 3 4 | import glob import os filenames_without_extension = [os.path.basename(c).split('.')[0:1][0] for c in glob.glob('your/files/dir/*.txt')] filenames_with_extension = [os.path.basename(c) for c in glob.glob('your/files/dir/*.txt')] |
这段代码使我的生活更简单。
1 2 3 4 5 6 | 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) |
我做了一个测试(python 3.6.4,w7x64),看看哪个解决方案对于一个文件夹(没有子目录)是最快的,以获得具有特定扩展名的文件的完整文件路径列表。
简而言之,对于这项任务,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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)) |
结果:
1 2 3 4 5 6 7 | # Python 3.6.4 0.431 0.515 0.161 0.548 0.537 0.274 |
以pythonic方式获取"datapath"文件夹中的所有".txt"文件名作为列表
1 2 3 4 5 | 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 |
使用fnmatch:https://docs.python.org/2/library/fnmatch.html
1 2 3 4 5 6 | import fnmatch import os for file in os.listdir('.'): if fnmatch.fnmatch(file, '*.txt'): print file |
Python V3.5+
在递归函数中使用os.scandir的快速方法。在文件夹和子文件夹中搜索具有指定扩展名的所有文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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) |
2019年4月更新
如果要搜索包含10000个文件的目录,则追加到列表将变得效率低下。产生结果是一个更好的解决方案。我还提供了一个将输出转换为熊猫数据帧的函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import os import re import pandas as pd import numpy as np def findFilesInFolderYield(path, extension, containsTxt='', subFolders = True, excludeText = ''): """ 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 extension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etc containsTxt: List of Strings, only finds file if it contains this text. Ignore if '' (or blank) subFolders: Bool. If True, find files in all subfolders under path. If False, only searches files in the specified folder excludeText: Text string. Ignore if ''. Will exclude if text string is in path. """ if type(containsTxt) == str: # if a string and not in a list containsTxt = [containsTxt] myregexobj = re.compile('\.' + extension + '$') # Makes sure the file extension is at the end and is preceded by a . try: # Trapping a OSError or FileNotFoundError: File permissions problem I believe for entry in os.scandir(path): if entry.is_file() and myregexobj.search(entry.path): # bools = [True for txt in containsTxt if txt in entry.path and (excludeText == '' or excludeText not in entry.path)] if len(bools)== len(containsTxt): yield entry.stat().st_size, entry.stat().st_atime_ns, entry.stat().st_mtime_ns, entry.stat().st_ctime_ns, entry.path elif entry.is_dir() and subFolders: # if its a directory, then repeat process as a nested function yield from findFilesInFolderYield(entry.path, extension, containsTxt, subFolders) except OSError as ose: print('Cannot access ' + path +'. Probably a permissions error ', ose) except FileNotFoundError as fnf: print(path +' not found ', fnf) def findFilesInFolderYieldandGetDf(path, extension, containsTxt, subFolders = True, excludeText = ''): """ Converts returned data from findFilesInFolderYield and creates and Pandas Dataframe. 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 extension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etc containsTxt: List of Strings, only finds file if it contains this text. Ignore if '' (or blank) subFolders: Bool. If True, find files in all subfolders under path. If False, only searches files in the specified folder excludeText: Text string. Ignore if ''. Will exclude if text string is in path. """ fileSizes, accessTimes, modificationTimes, creationTimes , paths = zip(*findFilesInFolderYield(path, extension, containsTxt, subFolders)) df = pd.DataFrame({ 'FLS_File_Size':fileSizes, 'FLS_File_Access_Date':accessTimes, 'FLS_File_Modification_Date':np.array(modificationTimes).astype('timedelta64[ns]'), 'FLS_File_Creation_Date':creationTimes, 'FLS_File_PathName':paths, }) df['FLS_File_Modification_Date'] = pd.to_datetime(df['FLS_File_Modification_Date'],infer_datetime_format=True) df['FLS_File_Creation_Date'] = pd.to_datetime(df['FLS_File_Creation_Date'],infer_datetime_format=True) df['FLS_File_Access_Date'] = pd.to_datetime(df['FLS_File_Access_Date'],infer_datetime_format=True) return df ext = 'txt' # regular expression containsTxt=[] path = 'C:\myFolder' df = findFilesInFolderYieldandGetDf(path, ext, containsTxt, subFolders = True) |
1 2 3 4 5 6 7 8 | import glob,os data_dir = 'data_folder/' file_dir_extension = os.path.join(data_dir, '*.txt') for file_name in glob.glob(file_dir_extension): if file_name.endswith('.txt'): print file_name |
为了我。这是经典之作。
我建议你使用FNMatch和Upper方法。通过这种方式,您可以找到以下任一项:
.
1 2 3 4 5 6 | import fnmatch import os for file in os.listdir("/Users/Johnny/Desktop/MyTXTfolder"): if fnmatch.fnmatch(file.upper(), '*.TXT'): print(file) |
您可以尝试以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | import glob import os os.chdir("D:\...\DirName") filename_arr={} i=0 for files in glob.glob("*.txt"): filename_arr[i] = files i= i+1 for key,value in filename_arr.items(): print key , value |
尝试此操作将找到文件夹或文件夹中的所有文件
1 2 3 4 5 6 7 | 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) |
如果文件夹包含大量文件或内存是一个约束,请考虑使用生成器:
1 2 3 4 5 | def yield_files_with_extensions(folder_path, file_extension): for _, _, files in os.walk(folder_path): for file in files: if file.endswith(file_extension): yield file |
选项A:迭代
1 2 | for f in yield_files_with_extensions('.', '.txt'): print(f) |
选项B:全部获取
1 | files = [f for f in yield_files_with_extensions('.', '.txt')] |
1 2 3 4 5 6 7 8 9 10 | import glob import os path=os.getcwd() extensions=('*.py','*.cpp') for i in extensions: for files in glob.glob(i): print files |
要从同一目录中名为"data"的文件夹中获取".txt"文件名数组,我通常使用以下简单代码行:
1 2 | import os fileNames = [fileName for fileName in os.listdir("data") if fileName.endswith(".txt")] |
一种类似于幽灵狗的复制意大利面食溶液:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 |
这是一个带
1 2 3 4 | types = ('*.jpg', '*.png') images_list = [] for files in types: images_list.extend(glob.glob(os.path.join(path, files))) |
一种使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | import os dir = ["e","x","e"] p = os.listdir('E:') #path for n in range(len(p)): name = p[n] myfile = [name[-3],name[-2],name[-1]] #for .txt if myfile == dir : print(name) else: print("nops") |
尽管这可以做得更笼统些。
带子目录的功能解决方案:
1 2 3 4 5 6 | from fnmatch import filter from functools import partial from itertools import chain from os import path, walk print(*chain(*(map(partial(path.join, root), filter(filenames,"*.txt")) for root, _, filenames in walk("mydir")))) |
使用python os模块查找具有特定扩展名的文件。
简单的例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import os # This is the path where you want to search path = r'd:' # this is extension you want to detect extension = '.txt' # this can be : .jpg .png .xls .log ..... for root, dirs_list, files_list in os.walk(path): for file_name in files_list: if os.path.splitext(file_name)[-1] == extension: file_name_path = os.path.join(root, file_name) print file_name print file_name_path # This is the full path of the filter file |
许多用户都用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import os def files_in_dir(path, extension=''): """ Generator: yields all of the files in <path> ending with <extension> \param path Absolute or relative path to inspect, \param extension [optional] Only yield files matching this, \yield [filenames] """ for _, dirs, files in os.walk(path): dirs[:] = [] # do not recurse directories. yield from [f for f in files if f.endswith(extension)] # Example: print all the .py files in './python' for filename in files_in_dir('./python', '*.py'): print("-", filename) |
或者对于一个你不需要发电机的地方:
1 2 3 4 5 6 7 | path, ext ="./python", ext =".py" for _, _, dirfiles in os.walk(path): matches = (f for f in dirfiles if f.endswith(ext)) break for filename in matches: print("-", filename) |
如果要对其他项使用匹配项,则可能需要将其设置为列表,而不是生成器表达式:
1 | matches = [f for f in dirfiles if f.endswith(ext)] |