我有以下文件夹结构:
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文档:
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文档中查找内容的第二个代码:
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()输出
['file001.docx',
'file002.docx',
'file003.docx',
'file004.docx',
'file005.docx',
'file006.docx',
'file007.docx',]由于os.walk()的递归性质。
但是,它抛出了一个错误:FileNotFoundError: [Errno 2] No such file or directory:
我在想我哪里出错了?谢谢!
发布于 2021-11-21 03:30:18
我想你应该把你的函数修改成这样来存储文件名和它们相关的路径。
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语句。
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])) https://stackoverflow.com/questions/70051525
复制相似问题