2013-04-15 179 views
7

我有代码工作读取单个文本文件的值,但是难以读取所有目录中的所有文件并将所有内容放在一起。Python:读取所有目录中的所有文件

以下是我有:

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 

请注意,我尽量坚持通配符*在文件名以及作为filesuffix通配符。我收到以下错误:

“IO错误:[错误2]没有这样的文件或目录:‘测试/ ’”

我不知道这是否是连做正确的方式,但似乎如果我以某种方式让通配符工作 - 它应该工作。

我已经得到了这个例子的工作:Python - reading files from directory file not found in subdirectory (which is there)

这是一个有点不同 - 但不知道如何更新它读取的所有文件。我想,在这第一组代码:

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

,我需要补充的东西在那里我有for循环外,但不太知道该怎么把它..

任何想法?

谢谢了,

布赖恩

回答

16

Python不直接支持文件名中的open()呼叫通配符。您需要使用glob module来代替从单个级别的子目录加载文件,或使用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. 
+0

谢谢你的Martijn了点。我会试试看看会发生什么。我很好奇,为什么他们做了两个不同的函数glob和os.walk。在一些小小的阅读中,我会看到glob会让你使用通配符,但os.walk不会 - 你需要过滤结果。我不明白到底发生了什么,因为当我想过滤结果时,我认为这是通配符表达式所做的。我发现这个职位: http://stackoverflow.com/questions/8931099/quicker-to-os-walk-or-glob 如果您有任何洞察力和时间,任何想法,赞赏。 – Relative0

+0

glob()不支持任意嵌套的子目录(还)。这是唯一的区别。 'os.walk()'确实需要更多的过滤。请注意,'glob()'在它自己的实现中使用了*相同的过滤器方法*('fnmatch'模块)。 –

相关问题