根据老婆的需求做的小程序:遍历文件夹下所有文件,如果有版本号则加入版本号,如果没有版本号则加入修改时间。最后返回一个字典,key是路径,value是版本号/修改时间。
具体代码如下:
importosimportos.pathimportwin32apiimporttimedefgetFileName(rootdir):
file=[]for parent, dirnames, filenames inos.walk(rootdir):for f infilenames:
file.append([parent, f])returnfiledefgetFileVersion(file_name):
info=win32api.GetFileVersionInfo(file_name, os.sep)
ms= info['FileVersionMS']
ls= info['FileVersionLS']
version= '%d.%d.%d.%04d' %(win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))returnversion
getFileTime= lambdafile: time.ctime(os.stat(file).st_mtime)#print getFileTime('d:/rizhi/12yue/ex121013.log')
res={}
rootdir= 'd:/rizhi'file=getFileName(rootdir)for n infile:
filePath= (n[0] + '/' + n[1]).replace('\\', '/')try:
version=getFileVersion(filePath)except:
modiTime=getFileTime(filePath)
res[filePath]=modiTimeelse:
res[filePath]=versionprint res
其中遍历文件夹部分参考了jihate的文章(http://www.cnblogs.com/kaituorensheng/archive/2012/08/14/2638935.html),获取版本号部分参考了Sephiroth的文章(http://www.oschina.net/code/snippet_16840_1863)。惭愧的是,后者的win32API还不懂。