Python:总结保存在不同文件夹中的xlsx文件的值

假设我有一个包含20个子文件夹的主文件夹。 每个子文件夹只包含一个xlsx文件。 我想总结位于每个xlsx文件的列A中的所有值,从而获得sub folder-sum value配对。

然后我想重复这个多次的主文件夹。

例:

 MAIN FOLDER 1 SUB FOLDER 1 SUB FOLDER 2 file1.xlsx file2.xlsx A1 17 A1 20 A2 32 A2 30 A3 24 A3 10 

相应的结果是:

 MAIN FOLDER 1 sum1 = 17+32+24 = 73 -> Pairing 1= Sub folder 1; 73 sum2 = 20+30+10 = 60 -> Pairing 2= Sub folder 2; 60 ... 

我写了一段代码,但我不确定for循环是正确的:

 import os from openpyxl import Workbook directoryPath=r'C:\Users\MyDir' os.mkdir(directoryPath) for root, dirs, files in os.walk(directoryPath): #This confuses me as I don't see how the main folders are differentiated from the sub folders for name in files: if name.endswith((".xlsx")): #summing up 

你的循环似乎正确。 os.walk会为迭代中的每个元素返回3个值,下一个目录,当前目录中的子目录以及当前目录中的文件列表。

在这个链接,你可以阅读正确的方式来使用os.walk

看下面的例子。 假设我有以下目录结构:

 +---main | | | +---sub1 | | f2.xls | | | \---sub2 | f1.xls 

这基本上是你现在的代码:

 for dirName, subdirList, fileList in os.walk(rootDir): print('Found directory: %s' % dirName) for fname in fileList: print('\t%s' % fname) 

在第一个循环中,您遍历主文件夹中的目录。 每次迭代都会代表您正在寻找的配对。 for fname in fileList的第二个循环仅列出存储在dirName中的文件夹中的文件,因此您不能将错误的文件夹和文件配对。 其实这是你的代码输出:

 Found directory: C:/Users/cr01046/Desktop/main Found directory: C:/Users/cr01046/Desktop/main\sub1 f2.xls Found directory: C:/Users/cr01046/Desktop/main\sub2 f1.xls