git 快速 查找冲突文件_Python小技巧|如何在win系统下快速查找文件

本文介绍了如何使用Python的os和glob模块遍历文件,但在大量文件时效率较低。推荐使用Everything这款Windows文件搜索神器,它提供了SDK以实现快速搜索。通过示例代码展示了如何用Python调用Everything SDK进行文件搜索,显著提高了查找速度。
摘要由CSDN通过智能技术生成

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

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

import osfrom os.path import join, getsizefor 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 ctypesimport datetimeimport struct#definesEVERYTHING_REQUEST_FILE_NAME = 0x00000001EVERYTHING_REQUEST_PATH = 0x00000002EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004EVERYTHING_REQUEST_EXTENSION = 0x00000008EVERYTHING_REQUEST_SIZE = 0x00000010EVERYTHING_REQUEST_DATE_CREATED = 0x00000020EVERYTHING_REQUEST_DATE_MODIFIED = 0x00000040EVERYTHING_REQUEST_DATE_ACCESSED = 0x00000080EVERYTHING_REQUEST_ATTRIBUTES = 0x00000100EVERYTHING_REQUEST_FILE_LIST_FILE_NAME = 0x00000200EVERYTHING_REQUEST_RUN_COUNT = 0x00000400EVERYTHING_REQUEST_DATE_RUN = 0x00000800EVERYTHING_REQUEST_DATE_RECENTLY_CHANGED = 0x00001000EVERYTHING_REQUEST_HIGHLIGHTED_FILE_NAME = 0x00002000EVERYTHING_REQUEST_HIGHLIGHTED_PATH = 0x00004000EVERYTHING_REQUEST_HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000#dll importseverything_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 searcheverything_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 queryeverything_dll.Everything_QueryW(1)#get the number of resultsnum_results = everything_dll.Everything_GetNumResults()#show the number of resultsprint("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-filetimeWINDOWS_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.0WINDOWS_TICKS_TO_POSIX_EPOCH = EPOCH_DIFF * WINDOWS_TICKS  # 116444736000000000.0def 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 buffersfilename = ctypes.create_unicode_buffer(260)date_modified_filetime = ctypes.c_ulonglong(1)file_size = ctypes.c_ulonglong(1)#show resultsfor 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_bufferdef 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 strBuffif __name__=='__main__':    for file in search_files('*.py'):        print (file) 

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

b40a8f68c37850290cac78ab7c749682.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用gitpython时,有时候会出现"bad git executable"或"export git_python_refresh=quiet"的问题。这个问题通常是由于git的执行文件git.exe或git)没有在系统的环境变量中被正确设置导致的。 要解决这个问题,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了git,并且git的执行文件已经被正确设置在系统的环境变量中。这通常可以在安装git时选择自动添加到系统环境变量中来实现。 2. 如果你已经安装了git但仍然遇到这个问题,可以尝试重新启动电脑,有时系统环境变量的更新需要重启才能生效。 3. 如果以上方法都没有解决问题,你可以尝试手动设置git的执行路径。首先,找到你的git执行文件所在的路径。然后,打开终端或命令提示符,输入以下命令来设置git的执行路径: export PATH=$PATH:/path/to/git 其中,/path/to/git是你的git执行文件所在的路径。 4. 如果你使用的是Windows操作系统,可以通过以下步骤手动设置系统环境变量: - 在桌面上右键点击"计算机"(或"此电脑"),选择"属性"。 - 在打开的窗口中,点击"高级系统设置"。 - 在"系统属性"窗口中,点击"环境变量"按钮。 - 在"系统变量"列表中找到"Path"变量,双击它打开编辑窗口。 - 在编辑窗口中,点击"新建"按钮,然后输入git的执行文件路径,点击"确定"保存设置。 通过以上操作,你应该能够成功解决"bad git executable"或"export git_python_refresh=quiet"的问题,并正常使用gitpython进行开发和项目管理。如有问题,可参考gitpython的官方文档或寻求技术支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值