QA:我需要找到像我搜索 "我的故事部分1.pdf"。我应该得到的建议,通过搜索" 故事1"。我已经分裂到 "故事"& "1"。这很好。但我怎么说 - 搜索这两个词在我的目录,并显示我作为建议,如果你得到任何文件至少有这些名称。这里可以有更多或更少的单词,但至少这些 我必须添加remove到代码?
Import os
word =input("Enter file name ")
words= word.split()
path =os.listdir('.')
for files in path :
for file in files :
如果file_name中含有列表词,则打印该文件。
if words **is in** file :
不知 是在 part.print 3个类似的文件.如果至少包含用户_input.则显示该文件。
print(file[-3:])
如果准确找到,则打开文件,添加打开过程。
elif word == file :
print("heres the file: ")
else :
print("no result")
它不像我想的那样工作,这里的文件是指路径中的文件标题,如果user_input至少包含他们的title_name,则显示为建议。Sep= ' ''.
读取输入并在sapce进行分割
word= input("Enter file name ")
如果你决定用os.listdir().而不是os.listdir,如果你在linux上,请参考这里的shell_output函数。
matches_found=False
similars=[]
for i in os.listdir('.'):
if i==word:
matches_found=True
print("found file - {}".format(i))
break
[similars.append(i) for x in word.split(" ") if x in i]
if similars!=[]:
print("Similar files -\n{}".format("\n".join(similars)))
exit(0)
if not matches_found:
print("No Such Files in this dir")
matches_found=False
similars=[]
for i in shell_output("ls").splitlines():
if i==word:
matches_found=True
print("found file - {}".format(i))
break
[similars.append(i) for x in word.split(" ") if x in i]
if similars!=[]:
print("Similar files -\n{}".format("\n".join(similars)))
exit(0)
if not matches_found:
print("No Such Files in this dir")
如果有帮助,请告诉我 :)