在Windows上,最好使用该os.walk
功能。os.walk
返回一个递归遍历源代码树的生成器。下面的示例显示了一个正则表达式搜索。
import os
import re
import win32api
def find_file(root_folder, rex):
for root,dirs,files in os.walk(root_folder):
for f in files:
result = rex.search(f)
if result:
print os.path.join(root, f)
break # if you want to find only one
def find_file_in_all_drives(file_name):
#create a regular expression for the file
rex = re.compile(file_name)
for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
find_file( drive, rex )
find_file_in_all_drives( 'myfile\.doc' )
一些注意事项: