使用此代码 我希望它搜索名称为 sh 的所有文件,如 sh.c、sh.txt、sh.cpp 等。但是除非我写 lookfor=sh.txt 或 ,否则这段代码不会搜索lookfor=sh.pdf 而不是下面代码中的 lookfor=sh 。 因此,我希望通过编写 lookfor=sh 来搜索所有名为 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
最佳答案
尝试 glob:
import glob
print glob.glob('sh.*') #this will give you all sh.* files in the current dir
关于python - 搜索文件python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155641/