读取所有目录中的所有文件 [英] Reading all files in all directories

查看:69
本文介绍了读取所有目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码可以读取单个文本文件的值,但是很难从所有目录中读取所有文件并将所有内容放在一起.

I have the code working to read in the values of a single text file but am having difficulties reading all files from all directories and putting all of the contents together.

这就是我所拥有的:

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

请注意,我试图将通配符*粘贴在文件名中,并将通配符粘贴在文件后缀中.我收到以下错误:

note that I am trying to stick the wildcard * in for the filename as well as the wildcard for the filesuffix. I get the following error:

"IOError:[Errno 2]没有这样的文件或目录:'Test/.'"

"IOError: [Errno 2] No such file or directory: 'Test/.'"

我不确定这是否是正确的方法,但似乎如果我以某种方式使通配符起作用,它应该起作用.

I am not sure if this is even the right way to do it but it seems that if I somehow get the wildcards working - it should work.

我已经使这个示例工作了:

I have gotten this example to work: Python - reading files from directory file not found in subdirectory (which is there)

有点不同-但不知道如何更新它以读取所有文件.我想在这组初始代码中:

Which is a little different - but don't know how to update it to read all files. I am thinking that in this initial set of code:

previous_dir = os.getcwd()
os.chdir('testfilefolder')
#add something here?
for filename in os.listdir('.'):

我需要在有外部for循环的地方添加一些内容,但不完全知道该放什么.

That I would need to add something where I have an outer for loop but don't quite know what to put in it..

有什么想法吗?

推荐答案

Python不直接在 open()调用的文件名中支持通配符.您需要使用 glob 模块来从单个子目录级别,或使用 os.walk() 遍历任意目录结构.

Python doesn't support wildcards directly in filenames to the open() call. You'll need to use the glob module instead to load files from a single level of subdirectories, or use os.walk() to walk an arbitrary directory structure.

打开所有子目录中的所有文本文件,深度达一级:

Opening all text files in all subdirectories, one level deep:

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.

在目录的任意嵌套中打开所有文本文件:

Opening all text files in an arbitrary nesting of directories:

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.

这篇关于读取所有目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆