Python 遍历子文件夹文件?

Python 遍历子文件夹文件?,python,python-3.x,Python,Python 3.x,我已经写了一个脚本来从docx文件中删除一个给定的单词,并且在检查子文件夹项目时也遇到了最后的困难。有人能帮我找出我执行失败的地方吗。它可以处理同一目录中的所有文件,但现在不会检查子文件夹项。谢谢你的帮助 #!/usr/bin/env python3 # Search and Replace all docx import os, docx from docx import Document findText = input("Type text to replace: ")

我已经写了一个脚本来从docx文件中删除一个给定的单词,并且在检查子文件夹项目时也遇到了最后的困难。有人能帮我找出我执行失败的地方吗。它可以处理同一目录中的所有文件,但现在不会检查子文件夹项。谢谢你的帮助

#!/usr/bin/env python3

# Search and Replace all docx

import os, docx

from docx import Document


findText = input("Type text to replace: ")                              

#replaceText = input('What text would you like to replace it with: ')    


for dirs, folders, files in os.walk('.'):
    for subDirs in dirs:
        print('The Sub is ' + subDirs)
        for fileNames in files:
            print(subDirs + fileNames)
            if fileNames.endswith('.docx'):
                newDirName = os.path.abspath(subDirs)
                fileLocation = subDirs + '\\' + fileNames
                document = docx.Document(fileLocation)
                print('Document is:' + fileLocation)

                tables = document.tables
                for table in tables:
                    for row in table.rows:
                        for cell in row.cells:
                            for paragraph in cell.paragraphs:
                                if findText in paragraph.text:                              
                                    inline = paragraph.runs                                 
                                    for i in range(len(inline)):
                                        if findText in inline[i].text:
                                            text = inline[i].text.replace(findText, '')
                                            inline[i].text = text

                for paragraph in document.paragraphs:                           
                    if findText in paragraph.text:                              
                        inline = paragraph.runs                                 
                        for i in range(len(inline)):
                            if findText in inline[i].text:
                                text = inline[i].text.replace(findText, '')
                                inline[i].text = text

                document.save(fileLocation)  
迭代子目录,为访问的每个子目录生成3元组
(dirpath、dirname、filename)
。当您这样做时:

for dirs, folders, files in os.walk('.'):
    for subDirs in dirs:
事情很糟糕
dirs
是每次迭代中的子目录名,这意味着dirs中的子目录的
实际上是在枚举目录名中的字符。碰巧您迭代的第一个目录是
”,碰巧它是一个单字符的目录名,所以for循环似乎可以工作

一旦您进入另一个子目录(我们称之为“foo”),您的代码将再次尝试查找名为
foo\f
foo\o
foo\o
的子目录。那不行

但是您不应该自己重新枚举子目录
os.walk
已经做到了这一点。将代码分解到枚举部分,将在子树中找到所有
.docx

#!/usr/bin/env python3

import os

for dirpath, dirnames, filenames in os.walk('.'):
    docx_files = [fn for fn in filenames if fn.endswith('.docx')]
    for docx_file in docx_files:
        filename = os.path.join(dirpath, docx_file)
        print(filename)

你说它也不会检查子文件夹项——这是否意味着实际的docx处理是不相关的。你能不能把那个样品剪得小一些,仍然不合格,但不会让我们眼睛疲劳
os.walk
遍历树,因此您可能不需要对目录中的子目录执行
只需第二次遍历子目录。
dirs
将是一个字符串,即当前目录,因此,当您在dirs中对subDirs执行
操作时,您正在迭代字符串中的单个字符。感谢大家的建议。嘿,tdelany,这是一个令人惊讶的解释,它工作得非常好。为眼睛疲劳道歉。你的解决方案是如此优雅的方法。如果你不介意的话,你可以把基线的名称分解一下。看起来你有我的多行代码,并且一次完成了所有的工作。再次感谢您的帮助。我扩展了操作并使用了合理的名称。不知道这是什么,但至少我知道要查找什么…哈。再次感谢。这是为了工作,所以可以节省我很多时间。