我目前正在执行此任务并陷入困境。请给我你的建议。我在下面写的代码,它只返回来自多个文件夹的 .txt 文件名,而我想打印出每个 .txt 文件的内容并保存在 excel 中的不同行中。每个 .txt 文件的内容都是数字。
import os, glob,xlwt
#create workbook and worksheet
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('data')
path= 'E:\Cag\Data'
row = 0
for dir,subdir,files in os.walk(path):
for file in files:
if glob.fnmatch.fnmatch(file, '*.txt'):
L = file.strip()
sheet.write(row,5,L)
row += 1
wbk.save('read_all_txt_in_folders.xls')
print 'success'