2015-04-07 48 views
0

我有找到字符串的平均长度的函数...Python的 - 在一个目录下的所有文件执行比较功能

def mean_length(sequence): 
    sum = 0.0 
    nonblank = 0 
    for string in sequence: 
     if string.strip(): 
      sum = sum + len(string) 
      nonblank = nonblank + 1 
    return sum/nonblank 

我有麻烦创建一个新的功能,将找到.txt文件与其他所有.txt文件相比,具有最高平均字符串长度的目录。任何帮助都会很棒,特别是如何比较目录中的所有文件。谢谢。

回答

0
path = # This will be the path to the directory from your current working driectory 

import glob 
files = glob.glob(path + '*.txt') # This returns a list containing all files that are in the path that end in ".txt" 
for file in files: 
    # Do what you need to do here for each file 

https://docs.python.org/2/library/glob.html

相关问题