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

我怎样才能find所有的文件在python扩展名为.txt的目录?

你可以使用glob

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

或者干脆os.listdir

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

或者如果你想遍历目录,使用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)) 

使用glob 。

 >>> import glob >>> glob.glob('./*.txt') ['./outline.txt', './pip-log.txt', './test.txt', './testingvim.txt'] 

像这样的东西应该做的工作

 for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.txt'): print file 

像这样的东西将工作:

 >>> 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'] 

我喜欢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 

或者与发电机:

 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 
 import os path = 'mypath/path' files = os.listdir(path) files_txt = [i for i in files if i.endswith('.txt')] 

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

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 

path.py是另一种select: 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 
 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 

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

 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有所有的工具来做到这一点:

 import os the_dir = 'the_dir_that_want_to_search_in' all_txt_files = filter(lambda x: x.endswith('.txt'), os.listdir(the_dir)) 

你可以试试这个代码

 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')] 

使用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 
 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 

为了我。 这是经典的。

你可以简单地使用pathlib s glob 1

 import pathlib list(pathlib.Path('your_directory').glob('*.txt')) 

或者在一个循环中:

 for txt_file in pathlib.Path('your_directory').glob('*.txt'): # do something with "txt_file" 

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


1 pathlib模块被包含在python 3.4的标准库中。 但是,即使在较早的Python版本(即使用condapip )上,您也可以安装该模块的后端端口: pathlibpathlib2

具有子目录的function解决scheme:

 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")))) 

如果文件夹包含很多文件或内存是一个约束,请考虑使用生成器:

 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:迭代

 for f in yield_files_with_extensions('.', '.txt'): print(f) 

选项B:全部获取

 files = [f for f in yield_files_with_extensions('.', '.txt')] 

我build议你使用fnmatch和上面的方法。 这样你可以find以下任何一个:

  1. 名称。 txt ;
  2. 名称。 TXT ;
  3. 名称。 文本

 import fnmatch import os for file in os.listdir("/Users/Johnny/Desktop/MyTXTfolder"): if fnmatch.fnmatch(file.upper(), '*.TXT'): print(file) 
 import glob import os path=os.getcwd() extensions=('*.py','*.cpp') for i in extensions: for files in glob.glob(i): print files 

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

 import os fileNames = [fileName for fileName in os.listdir("data") if fileName.endswith(".txt")] 

你可以试试这个代码:

 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 

使用Python OS模块来查找具有特定扩展名的文件。

简单的例子是这里:

 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 

试试这个会发现你的文件夹或文件夹里面的所有文件

 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) 

与ghostdog类似的可复制解决scheme:

 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 

许多用户回答os.walk答案,其中包括所有的文件,但也包括所有的目录和子目录及其文件。

 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) 

或者在不需要发电机的地方使用一个:

 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) 

如果你打算使用匹配的东西,你可能想把它作为一个列表,而不是一个生成器expression式:

  matches = [f for f in dirfiles if f.endswith(ext)] 

这里有一个extend()

 types = ('*.jpg', '*.png') images_list = [] for files in types: images_list.extend(glob.glob(os.path.join(path, files))) 
 import os [x for x in os.listdir() if x.endswith(".txt")] 

在DIR和SUBDIRS中有多less文件?

如果你想知道在一个目录和子目录中有多less个文件:

在这个例子中,我们查找包含在所有目录及其子目录中的文件数量。

 import os def count(dir, counter=0): "returns number of files in dir and subdirs" for pack in os.walk(dir): for f in pack[2]: counter += 1 return dir + " : " + str(counter) + "files" print(count("F:\\python")) 

产量

'F:\ python':12057 files'