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


#1楼

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

#2楼

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

#3楼

此代码使我的生活更简单。

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

#4楼

import ospath = 'mypath/path'
files = os.listdir(path)files_txt = [i for i in files if i.endswith('.txt')]

#5楼

带有子目录的功能解决方案:

from fnmatch import filter
from functools import partial
from itertools import chain
from os import path, walkprint(*chain(*(map(partial(path.join, root), filter(filenames, "*.txt")) for root, _, filenames in walk("mydir"))))

#6楼

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

import fnmatch
import osfor file in os.listdir('.'):if fnmatch.fnmatch(file, '*.txt'):print file

#7楼

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

import glob, os
os.chdir("H:\\wallpaper")# use whatever you directory #double\\ no single \for file in glob.glob("**/*.psd", recursive = True):#your formatprint(file)

#8楼

使用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_nameprint file_name_path   # This is the full path of the filter file

#9楼

Python具有执行此操作的所有工具:

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

#10楼

使用glob 。

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

#11楼

这样的事情应该做的

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

#12楼

您可以使用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))

#13楼

这样的事情会起作用:

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

#14楼

我喜欢os.walk() :

import os, os.pathfor 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.pathfileiter = (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

#15楼

以下是相同版本的更多版本,它们会产生稍微不同的结果:

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

#16楼

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

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

#17楼

我建议您使用fnmatch和upper方法。 这样,您可以找到以下任意一项:

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

import fnmatch
import osfor file in os.listdir("/Users/Johnny/Desktop/MyTXTfolder"):if fnmatch.fnmatch(file.upper(), '*.TXT'):print(file)

#18楼

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

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

#19楼

您可以简单地使用pathlibglob 1

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

或循环:

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

如果您希望递归,则可以使用.glob('**/*.txt)


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


#20楼

可复制的解决方案,类似于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 : strext : strReturns-------list of strExamples-------->>> 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 osall_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

#21楼

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

import osdef 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))breakfor filename in matches:print("-", filename)

如果要将匹配用于其他内容,则可能需要使其成为列表而不是生成器表达式:

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

#22楼

这是带有extend()的一个

types = ('*.jpg', '*.png')
images_list = []
for files in types:images_list.extend(glob.glob(os.path.join(path, files)))

#23楼

使用for循环的一种简单方法:

import osdir = ["e","x","e"]p = os.listdir('E:')  #pathfor n in range(len(p)):name = p[n]myfile = [name[-3],name[-2],name[-1]]  #for .txtif myfile == dir :print(name)else:print("nops")

虽然这可以使它更加笼统。


#24楼

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

简而言之,此任务os.listdir()是最快的,并且是次pathlib倍: os.walk() (有间断!),是pathlib 2.7倍,比pathlibpathlib倍。 os.scandir()glob快3.3倍。
请记住,当您需要递归结果时,这些结果将改变。 如果您复制/粘贴以下一种方法,请添加.lower(),否则在搜索.ext时找不到.EXT。

import os
import pathlib
import timeit
import globdef 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) )breakprint(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))

结果:

# Python 3.6.4
0.431
0.515
0.161
0.548
0.537
0.274

#25楼

以Python方式将“ dataPath”文件夹中的所有“ .txt”文件名作为列表获取

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

#26楼

Python v3.5 +

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

import osdef 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 filespathList:    A list that stores all pathsextension:   File extension to findsubFolders:  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 believefor 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 functionpathList = findFilesInFolder(entry.path, pathList, extension, subFolders)except OSError:print('Cannot access ' + path +'. Probably a permissions error')return pathListdir_name = r'J:\myDirectory'
extension = ".txt"pathList = []
pathList = findFilesInFolder(dir_name, pathList, extension, True)

2019年4月更新

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

import os
import re
import pandas as pd
import numpy as npdef 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 filesextension:          File extension to find.  e.g. 'txt'.  Regular expression. Or  'ls\d' to match ls1, ls2, ls3 etccontainsTxt:        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 folderexcludeText:        Text string.  Ignore if ''. Will exclude if text string is in path."""if type(containsTxt) == str: # if a string and not in a listcontainsTxt = [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 believefor 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.pathelif entry.is_dir() and subFolders:   # if its a directory, then repeat process as a nested functionyield 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 filesextension:          File extension to find.  e.g. 'txt'.  Regular expression. Or  'ls\d' to match ls1, ls2, ls3 etccontainsTxt:        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 folderexcludeText:        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 dfext =   'txt'  # regular expression
containsTxt=[]
path = 'C:\myFolder'
df = findFilesInFolderYieldandGetDf(path,  ext, containsTxt, subFolders = True)

在Python中以扩展名.txt查找目录中的所有文件相关推荐

  1. 使用Python,OpenCV,K-Means聚类查找图像中最主要的颜色

    Python,OpenCV,K-Means聚类查找图像中最主要的颜色 1. K-Means是什么? 2. 步骤 3. 效果图 4. 源代码 参考 对于肉眼来说,从一幅图中识别出主要颜色很容易.那怎么用 ...

  2. Asp.Net中修改扩展名的问题

    关于Asp.Net中的扩展名修改问题,方法有很多种,而且有比较成熟的第三方组件.这方面的东西在老赵点滴上说的很明白.我在这里给大家介绍一种比较简单的方法通过配置IIS来实现. Framework1.1 ...

  3. php ziparc 扩展_请问扩展名为.php.cfg.pdf.rar的文件分别是什么文件用什么软件能够打开...

    请问扩展名为.php.cfg.pdf.rar的文件分别是什么文件用什么软件能够打开 补充说明:请问,扩展名为.php.cfg.pdf.rar的文件分别是什么文件?用什么软件能够打开? 更新时间:201 ...

  4. 扩展名不见了?怎么批量给文件添加扩展名?

    概要:有时候由于我们的误操作,会导致我们本地文件的扩展名丢失,或者网上下载下来的文件本来就没有扩展名.这样会导致我们在打开文件的时候,系统不知道该用什么软件去打开这些文件.那我们怎么给这些扩展名不见的 ...

  5. matlab 动态目录调用程序集,C#中如何动态添加程序集查找目录

    C#中如何动态添加程序集查找目录 情况如下: 现有三个程序集Main.exe, One.dll, Two.dll.其中One.dll引用了Two.dll, 并且One.dll与Two.dll部署在一起 ...

  6. linux查找目录中指定文件或遍历指定文件夹

    查找目录中指定文件 1.终端输入:find 指定文件夹 指定文件名 2.终端输入:ll 指定文件夹 其中,文件夹可以不指定,但搜索较慢.ll主要是遍历比较快.

  7. 如何在Windows中的命令提示符下删除特定目录中的文件/子文件夹

    本文翻译自:How to delete files/subfolders in a specific directory at the command prompt in Windows Say, t ...

  8. python程序的扩展名是perl程序的扩展名是_Python 程序扩展名(py, pyc, pyw, pyo, pyd)及发布程序时的选择...

    扩展名 在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名.下面是几种扩展名的用法. py py就是最基本的源码扩展名.windows下直接双击运行会调用python.ex ...

  9. python的脚本扩展名是什么_Python的脚本文件扩展名为()。

    [单选题]在Python中可使用readline([size])来读取文件中的数据,如果参数size省略,则读取文件中的(). [单选题]佳能的标志属于哪种形式? [单选题]在open函数中访问模式参 ...

最新文章

  1. Android模拟器安装程序及上传音乐并播放
  2. 怎么定义html的整体的宽度,html怎么设置最大宽度
  3. C语言Kruskal 算法 (MST)(附完整源码)
  4. CodeForces - 1066C Books Queries(思维)
  5. ThreadLocal介绍以及源码分析
  6. 亿嘉和机器人上市了吗_亿嘉和上半年收入持续增长,拟7亿元定增加码主业研发...
  7. Save the Room【找规律】
  8. Javascript第五章history对象第四课
  9. zip命令通过yum安装和使用方法
  10. 检测网络变化(wifi、2g、3g、4g)
  11. fasthttp中的协程池实现
  12. linux文件系统与磁盘(五)分区的取消挂载、调整分区大小
  13. 1068 万绿丛中一点红(20)
  14. html画布刮刮乐,h5canvas实现刮刮乐效果的方法
  15. 关于Ubuntu14.04拼音不能正常使用的解决方案
  16. 2017.1直播类APP排行:斗鱼第一、YY第二、映客第三
  17. linux系统查看进程
  18. 卫星环绕地球c语言编程,动画技术——卫星环绕地球
  19. Python 学习笔记 元组 xxxxxxx XXXXXXXXXX
  20. SiTime硅晶振和石英晶振的冲击和振动性能比较

热门文章

  1. 调试技巧之 找准调试点
  2. 【剑指offer-Java版】25二叉树中和为某一值的路径
  3. 屏幕旋转导致Activity销毁重建,ViewModel是如何恢复数据的
  4. (0060)iOS开发之iOS 9: UIStackView入门
  5. 软件系统非功能测试方法,非功能测试方案模板
  6. Android相关面试题---初识
  7. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法...
  8. Remove Nth Node From End of List - LeetCode
  9. 2018-08-19-Python全栈开发day43-正反选练习
  10. 前端开发工程师 - 04.页面架构 - CSS Reset 布局解决方案 响应式 页面优化 规范与模块化...