Python小技巧|如何在win系统下快速查找文件

在工作的时候有时需要去处理一些文件,如果不在一个文件夹里面会去遍历整个盘符(如F盘),这个时候手动查找和搜索显得非常慢,单个还好,如果多个,就不得不写程序来处理了。

据我所知,Python有两个函数可以遍历文件夹(包括子文件夹),os模块的walk函数,以及glob模块的glob函数,其中os.walk函数,查看help文档有示例代码:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print(root, "consumes", end="")
    print(sum([getsize(join(root, name)) for name in files]), end="")
    print("bytes in", len(files), "non-directory files")
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

可以直接拿来用,而glob.glob函数虽然没提供示例,但help文档也很清晰:

glob(pathname, *, recursive=False)
    Return a list of paths matching a pathname pattern.
    
    The pattern may contain simple shell-style wildcards a la
    fnmatch. However, unlike fnmatch, filenames starting with a
    dot are special cases that are not matched by '*' and '?'
    patterns.
    
    If recursive is true, the pattern '**' will match any files and
    zero or more directories and subdirectories.

不难理解,第二个参数为**,且第三个参数为recursive=True时,即可以遍历指定的路径(包含子文件夹):

glob(pathname, **, recursive=True)

但是很遗憾的是,这两个函数在遍历文件和子文件夹比较多的文件夹时,会显非常慢,如果你使用的是 win系统,则可以尝试另外的方式。

很多朋友应该听过 Everything 这个查找神器,下载地址:

https://www.voidtools.com/zh-cn/downloads/

它在win系统下搜索文件可以说非常的快速,更多介绍请看这里:

https://www.voidtools.com/zh-cn/faq/

那怎么写程序来调用呢?它提供了SDK:

http://www.voidtools.com/support/everything/sdk/

函数非常的多,也给了Python的调用示例:

import ctypes
import datetime
import struct


#defines
EVERYTHING_REQUEST_FILE_NAME = 0x00000001
EVERYTHING_REQUEST_PATH = 0x00000002
EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004
EVERYTHING_REQUEST_EXTENSION = 0x00000008
EVERYTHING_REQUEST_SIZE = 0x00000010
EVERYTHING_REQUEST_DATE_CREATED = 0x00000020
EVERYTHING_REQUEST_DATE_MODIFIED = 0x00000040
EVERYTHING_REQUEST_DATE_ACCESSED = 0x00000080
EVERYTHING_REQUEST_ATTRIBUTES = 0x00000100
EVERYTHING_REQUEST_FILE_LIST_FILE_NAME = 0x00000200
EVERYTHING_REQUEST_RUN_COUNT = 0x00000400
EVERYTHING_REQUEST_DATE_RUN = 0x00000800
EVERYTHING_REQUEST_DATE_RECENTLY_CHANGED = 0x00001000
EVERYTHING_REQUEST_HIGHLIGHTED_FILE_NAME = 0x00002000
EVERYTHING_REQUEST_HIGHLIGHTED_PATH = 0x00004000
EVERYTHING_REQUEST_HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000


#dll imports
everything_dll = ctypes.WinDLL ("C:\\EverythingSDK\\DLL\\Everything32.dll")
everything_dll.Everything_GetResultDateModified.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_ulonglong)]
everything_dll.Everything_GetResultSize.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_ulonglong)]


#setup search
everything_dll.Everything_SetSearchW("test.py")
everything_dll.Everything_SetRequestFlags(EVERYTHING_REQUEST_FILE_NAME | EVERYTHING_REQUEST_PATH | EVERYTHING_REQUEST_SIZE | EVERYTHING_REQUEST_DATE_MODIFIED)


#execute the query
everything_dll.Everything_QueryW(1)


#get the number of results
num_results = everything_dll.Everything_GetNumResults()


#show the number of results
print("Result Count: {}".format(num_results))


#convert a windows FILETIME to a python datetime
#https://stackoverflow.com/questions/39481221/convert-datetime-back-to-windows-64-bit-filetime
WINDOWS_TICKS = int(1/10**-7)  # 10,000,000 (100 nanoseconds or .1 microseconds)
WINDOWS_EPOCH = datetime.datetime.strptime('1601-01-01 00:00:00',
                                           '%Y-%m-%d %H:%M:%S')
POSIX_EPOCH = datetime.datetime.strptime('1970-01-01 00:00:00',
                                         '%Y-%m-%d %H:%M:%S')
EPOCH_DIFF = (POSIX_EPOCH - WINDOWS_EPOCH).total_seconds()  # 11644473600.0
WINDOWS_TICKS_TO_POSIX_EPOCH = EPOCH_DIFF * WINDOWS_TICKS  # 116444736000000000.0


def get_time(filetime):
    """Convert windows filetime winticks to python datetime.datetime."""
    winticks = struct.unpack('<Q', filetime)[0]
    microsecs = (winticks - WINDOWS_TICKS_TO_POSIX_EPOCH) / WINDOWS_TICKS
    return datetime.datetime.fromtimestamp(microsecs)


#create buffers
filename = ctypes.create_unicode_buffer(260)
date_modified_filetime = ctypes.c_ulonglong(1)
file_size = ctypes.c_ulonglong(1)


#show results
for i in range(num_results):


  everything_dll.Everything_GetResultFullPathNameW(i,filename,260)
  everything_dll.Everything_GetResultDateModified(i,date_modified_filetime)
  everything_dll.Everything_GetResultSize(i,file_size)
  print("Filename: {}\nDate Modified: {}\nSize: {} bytes\n".format(ctypes.wstring_at(filename),get_time(date_modified_filetime),file_size.value))

显得比较难以理解,我自己照着其他的示例写了个简单易理解的,代码如下:

from ctypes import windll,byref,create_unicode_buffer


def search_files(file):
    Search = windll.LoadLibrary("everything64.dll")
    strBuff = create_unicode_buffer(255)
    
    Search.Everything_SetSearchW(file)
    Search.Everything_QueryW(True)
    
    Results = Search.Everything_GetNumResults()
 
    for index in range(Results):
        Search.Everything_GetResultFullPathNameW(index,byref(strBuff),len(strBuff))
        yield strBuff.value
 
    del Search
    del strBuff


if __name__=='__main__':
    for file in search_files('*.py'):
        print (file) 

在调用它的SDK时,网站上也很贴心的给了我们一些注意事项:

简而言之就是在调用的时候,一定要打开 Everything 这个软件。更多的功能请自己去发现吧^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值