您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python搜索所有驱动器中的文件

使用Python搜索所有驱动器中的文件

在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' )

一些注意事项:

python 2022/1/1 18:34:11 有213人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶