码迷,mamicode.com
首页 > 编程语言 > 详细

python查询windows文件

时间:2018-07-23 20:51:19      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:mount   python   获取   os.path   nbsp   device   ice   判断   列表   

闲着没事,使用python写了一个查询windows中盘符文件的程序

代码:

import os
import winreg

class Find_assign_file:
    """
    drive 要查找的主路径,这是一个列表
    file_find 要查找的文件名
    file_mode 查找形式
    信息为:
        ‘i‘ 无误查找,根据用户输入的完整的文件名以及后缀信息进行查找
        ‘h‘ 半模糊查找,根据用户输入的不包括后缀信息的文件名进行查找
        ‘d‘ 模糊查找,根据用户输入的部分文件名信息进行查找
        以上三种模式全部是根据用户名进行查找,使用时应与下面的mode=‘f‘关联使用
    mode 查找模式
        ‘f‘ 文件名查找,根据用户输入的文件名信息对相应路径下寻找特定文件
        ‘c‘ 文件内容查找,根据用户输入的文件中的信息查找相应的可能文件
    """
    def __init__(self, drive, file_find, file_mode, mode=‘‘):
        self._subKey = SYSTEM\MountedDevices
        self.drive = drive
        self.file_find = file_find
        self.file_mode = file_mode
        self.mode = mode

    def GetPartitionNames(self):
        ‘‘‘通过注册表获取系统盘符信息,并返回一个包含盘符信息的列表‘‘‘
        res = []
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self._subKey)
        i = 0
        try:
            while True:
                name, value, type = winreg.EnumValue(key, i)
                if name.startswith(\\DosDevices\\):
                    res.append((name, repr(value)[1:17]))
                i += 1
        except WindowsError:
            pass
        res = [i[0][12:] + / for i in res]
        return res

    def find_begin(self, path):
        """
        递归遍历每一个文件夹
        :param path:
        :return:
        """
        for file_path in path:
            try:
                if os.path.isdir(file_path):  # 如果是目录
                    list = os.listdir(file_path)
                    if self.file_mode == f:
                        # 用户查找特定目录
                        self.find_input(list, file_path)
                    if len(list) != 0:  # 如果目录不为空
                        list = [file_path + / + j for j in list]
                        # 递归
                        self.find_begin(list)
                    else:
                        # print(‘{}---目录为空‘.format(file_path))
                        pass
                else:
                    # 用户选择为根据文件内容进行查找特定文件
                    if self.file_mode == c:
                        with open(file_path, r, encoding=utf-8, errors=ignore) as f:
                            content = f.read()
                            if self.file_find in content:
                                print(file_path)
                    else:
                        pass
            except NotADirectoryError:
                # print(‘{}---无效‘.format(file_path))
                pass
            except PermissionError:
                # print(‘{}---拒绝访问‘.format(file_path))
                pass

    def find_input(self, list, old_file_path):
        """
        根据用户的选择的mode进行相应的操作
        :param list:
        :param old_file_path:
        :return:
        """
        for file_name in list:
            # 根据完整的文件名加后缀查找文件,输出文件绝对路径
            if self.mode == i:
                if file_name == self.file_find:
                    print(---, old_file_path + / + file_name)
            # 根据完整的文件名进行查找,输出文件绝对路径
            elif self.mode == h:
                if file_name.split(.)[0] == self.file_find:
                    print(---, old_file_path + / + file_name)
            # 根据不完整的文件名进行查找,输出文件绝对路径
            elif self.mode == d:
                if self.file_find in file_name:
                    print(---, old_file_path + / + file_name)

    def get_every_file(self):
        """
        1.判断是否为用户输入路径
        2.如果使用用户输入的路径,则直接调用执行函数
        3.如果用户没有输入,则默认为系统全部盘符
        """
        if self.drive == ‘‘:
            self.drive = self.GetPartitionNames()
        else:
            self.drive = [self.drive]
        # 参数为一个包含路径信息的列表
        self.find_begin(self.drive)

def run():
    drive = input("要查找的文件主路径[‘path‘] 不输入则查询所有盘符:")
    file_find = input("请输入文件名字或者文件中的部分内容:")
    file_mode = input("查找文件请输入f-查找内容文件请输入c:")
    if file_mode == f:
        mode = input("模式(i:完整文件名以及后缀)(h:完整文件名)(d:部分文件名):")
        path = Find_assign_file(drive, file_find, file_mode, mode)
    else:
        path = Find_assign_file(drive, file_find, file_mode)
    path.get_every_file()

if __name__ == "__main__":
    run()

部分使用说明:

1.使用完整文件名以及后缀查找文件名

技术分享图片

2.使用完整文件名

技术分享图片

3.使用部分文件名信息

技术分享图片

4.通过文件内容查找文件名信息

技术分享图片

共有以上几种方法

 

python查询windows文件

标签:mount   python   获取   os.path   nbsp   device   ice   判断   列表   

原文地址:https://www.cnblogs.com/hong976737/p/9356714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!