阅读所有目录中的所有文件
阅读所有目录中的所有文件
这个问题已经有了答案:
我已经成功编写了一个读取单个文本文件中值的代码,但是在尝试读取所有目录的所有文件并将所有内容放在一起时遇到了困难。
这是我拥有的:
filename = '*' filesuffix = '*' location = os.path.join('Test', filename + "." + filesuffix) Document = filename thedictionary = {} with open(location) as f: file_contents = f.read().lower().split(' ') # split line on spaces to make a list for position, item in enumerate(file_contents): if item in thedictionary: thedictionary[item].append(position) else: thedictionary[item] = [position] wordlist = (thedictionary, Document) #print wordlist #print thedictionary
请注意,我试图将通配符*用于文件名,以及文件后缀的通配符。我得到以下错误:
\"IOError:[Errno 2]没有这样的文件或目录:“Test /。”\"
我不确定这是否是正确的方法,但是如果我某种方式使通配符工作-它应该工作。
我已经让这个例子工作了:Python-读取目录中的文件,子目录中找不到文件(存在)
有点不同-但不知道如何更新它来读取所有文件。我认为在此初始代码中:
previous_dir = os.getcwd() os.chdir('testfilefolder') #add something here? for filename in os.listdir('.'):
我需要添加一个外部循环,但不知道该放什么..
有什么想法吗?
admin 更改状态以发布 2023年5月20日
Python在调用open()
函数时不直接支持通配符。你需要使用glob
模块来从单个子目录中加载文件,或者使用os.walk()
来遍历任意目录结构。打开所有一级子目录中的所有文本文件:
import glob for filename in glob.iglob(os.path.join('Test', '*', '*.txt')): with open(filename) as f: # one file open, handle it, next loop will present you with a new file.
在任意嵌套目录中打开所有文本文件:
import os import fnmatch for dirpath, dirs, files in os.walk('Test'): for filename in fnmatch.filter(files, '*.txt'): with open(os.path.join(dirpath, filename)): # one file open, handle it, next loop will present you with a new file.