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

网友投稿 789 2022-05-28

在工作的时候有时需要去处理一些文件,如果不在一个文件夹里面会去遍历整个盘符(如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 '?'

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

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('

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 这个软件。更多的功能请自己去发现吧^_^

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java--第9章 输入输出流
下一篇:新型存储介质对数据管理的影响
相关文章