在工作的时候有时需要去处理一些文件,如果不在一个文件夹里面会去遍历整个盘符(如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时,网站上也很贴心的给了我们一些注意事项:
简而言之就是在调用的时候,一定要打开 Everything 这个软件。更多的功能请自己去发现吧^_^