使用此代码 我要它搜索所有名为sh的文件,比如sh.c,什.txt,上海cpp等等,但是除非我编写查找,否则此代码不会搜索=什.txt或寻找=上海.pdf而不是下面代码中的lookfor=sh。 因此,我希望通过编写lookfor=sh来搜索所有名为嘘,拜托救命啊。在
import os
from os.path import join
lookfor = "sh"
for root, dirs, files in os.walk('C:\\'):
if lookfor in files:
print "found: %s" % join(root, lookfor)
break
假设您想搜索以sh作为其基名的文件。(名称中不包括路径和扩展名的部分)您可以使用
filter
模块中的filter
函数来实现这一点。在试试地球仪:
替换:
有:
^{pr2}$什么
filename.rsplit('.', 1)[0]
是删除文件最右边的一个点(=扩展名)后面的部分。如果文件中有几个点,我们将其余的保留在文件名中。在相关问题 更多 >
编程相关推荐