文章目录构想文件夹结构读取希望递归遍历的文件
想法
首先遍历父文件夹下的所有内容,如果内容是文件【其他条件,例如读取PDF、Excel】,则直接读取,如果不是文件,则递归重复上一操作。
不用说文件的结构。 上菜。 首先,让我给你看看文件的结构。
实现代码如下:
print(OS.getcwd(.rsplit(( ') [-1] ) forIinOS.listdir ) OS.getcwd ) ) ) 3360ifOS.path.isdir ) ) t{}.format(I ) ) format
对应的代码如下。
importosimportdocximportpandasaspddefread _ word (cur _ dir ) : for sub _ file inos.list dir (cur _ dir ) 33365292; 遍历此文件夹下的每个文件或文件夹sub _ file _ ABS _ path=OS.path.join (cur _ dir, sub_file ) #可能已拼写为完整路径,并且完整的pathfile _ path=OS.path.join (cur )便于稍后使用ifOS.path.isfile(sub_file ) ) 确定是否=='docx':#docx文件FP=docx.document ) content=' forpinfp.paragraphs 3360 content=p.text data.locument=[file_path.rsplit (() () () ) ) ) content (else : sub _ folder _ path=OS.path.join (cur _ path.join ) 想法的来源感谢jmdhf。