首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从子文件夹中检索文件名

如何从子文件夹中检索文件名
EN

Stack Overflow用户
提问于 2021-11-21 03:10:13
回答 1查看 43关注 0票数 0

我有以下文件夹结构:

代码语言:javascript
复制
root
│   file001.docx
│   file002.docx    
│
└───folder1
   │   file003.docx
   │   file004.docx
   │
   └───subfolder1
       │   file005.docx
       │   file006.docx
       |____subfolder2
            |
            |_file007.docx

我希望创建一个程序,当有人键入他们的根目录和关键字,文件将显示。例如:如果我输入"hello there!",file007.docx就会显示出来(假设文本是"hello there!“包含在file007.docx中),并让用户知道键入的单词在单词doc中。

为此,我使用以下代码列出了文件夹和子文件夹中的所有word文档:

代码语言:javascript
复制
def find_doc():
    variable= input('What is your directory?') #asking for root directory
    os.chdir(variable)
    files = []
    for dirpath, dirnames, filenames in os.walk(variable):
        for filename in [f for f in filenames if f.endswith(".docx")]:
            files.append(filename)  
    return files

现在,这是在每个word文档中查找内容的第二个代码:

代码语言:javascript
复制
all_files= find_doc() # just calling the first function I just made

while True: 
    keyword= input('Input your word or type in Terminate to exit: ')
    for i in range(len(all_files)): 
        text = docx2txt.process(all_files[i]) 
        if keyword.lower() in text.lower():  #to make it case insensitive
            print ((all_files[i]))    
    if keyword== ('Terminate') or keyword== ('terminate'):
        break

理论上,如果我在input:input('Input your word or type in Terminate to exit: ')中输入单词"hello",我应该能够检索file007.docx,因为all_files= find_doc()输出

代码语言:javascript
复制
['file001.docx',
'file002.docx',
'file003.docx',
'file004.docx',
'file005.docx',
'file006.docx',
'file007.docx',]

由于os.walk()的递归性质。

但是,它抛出了一个错误:FileNotFoundError: [Errno 2] No such file or directory:

我在想我哪里出错了?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-21 03:30:18

我想你应该把你的函数修改成这样来存储文件名和它们相关的路径。

代码语言:javascript
复制
def find_doc():
    variable= input('What is your directory?') #asking for root directory
    os.chdir(variable)
    files = []
    for dirpath, dirnames, filenames in os.walk(variable):
        for filename in [f for f in filenames if f.endswith(".docx")]:
            files.append(os.path.join(dirpath, filename))
    return files

您还应该更改while循环,以便在运行for循环之前检查if语句。

代码语言:javascript
复制
while True: 
    keyword= input('Input your word or type in Terminate to exit: ')
    if keyword.lower() == 'terminate':
        break
    else:   
        for i in range(len(all_files)): 
            text = docx2txt.process(all_files[i]) 
            if keyword.lower() in text.lower():  #to make it case insensitive
                print ((all_files[i]))  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70051525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档