我正在尝试设置一个程序,在其中有输入的内容,将在windows中搜索硬盘(c:\)。我输入了下面的代码,但它采用lookfor变量中的完整路径。我在想,如果我有几个字母匹配,我该如何设置它们。例如,如果我把Curr放在lookfor变量中,我希望它能找到Current1.png,但我需要输入完整的项目(Current1.png)来定位它
import os
from os.path import join
lookfor = 'Current1.png'
for root, dirs, files in os.walk('c:\\'):
#print ('Searching', root)
if lookfor in files:
print ('Found: %s' %join(root, lookfor))
break
问题是
os.walk()
正在返回文件列表,而您正在该文件列表中搜索字符串如果您在该列表中搜索“Curr”,则不会找到它
尝试将代码更改为
相关问题 更多 >
编程相关推荐