试试这个:
import glob, os path = r"c:\Users\xf01145\Downloads" os.chdir(path) fileList = [] result = [] for file in glob.glob("*.txt"): fileList.append(file) for file in fileList: with open(file) as f: text = f.read().splitlines() # list of lines lines = len(text) # length of the list = number of lines words = sum(len(line.split()) for line in text) # split each line on spaces, sum up the lengths of the lists of words characters = sum(len(line) for line in text) # sum up the length of each line result.append([file, lines, words, characters]) for i,y in enumerate(result): print(i, y)