网站开发支持二次开发,国外网站建站,晋城 网站建设,php网站开发工程师面试本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法。分享给大家供大家参考。具体如下#xff1a;功能代码如下#xff1a;import os, string, shutil,reimport pefileimport codecs, sysimport wximport struct#输出中打印Unicode字符#sys.stdout co…本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法。分享给大家供大家参考。具体如下功能代码如下import os, string, shutil,reimport pefileimport codecs, sysimport wximport struct#输出中打印Unicode字符#sys.stdout codecs.lookup(utf-8)[-1](sys.stdout)def addToDict(theDict,PEfile_Path,strCompanyName):theDict.setdefault(PEfile_Path, [ ]).append(strCompanyName)#存在就在基础上加入列表不存在就新建个字典keydef IsPeFile(inputFileName):判断一个文件是否为PE文件file open(inputFileName, r)dosSign hex(struct.unpack(h,file.read(2))[0])if (dosSign 0x5a4d):file.seek(0x3c)date_fNew struct.unpack(l,file.read(4))[0]file.seek(date_fNew)peSign hex(struct.unpack(h,file.read(2))[0])if (peSign 0x4550):return 1else:return 0else:return 0#得到一个文件的厂商信息#输入文件路径#输出字典def getCompanyName(PEfile_Path):if not IsPeFile(PEfile_Path):return {}else:dictCompany {}pe pefile.PE(PEfile_Path)p re.compile(CompanyName:(.))for name in p.findall(pe.__str__()):uniCompanyName name.replace(\\x, \\u).strip()#strTemp uniCompanyName.decode(unicode_escape)addToDict(dictCompany, PEfile_Path, uniCompanyName)writeDicToFile(dictCompany) #写入文件return dictCompany#得到文件夹中所有文件的厂商信息#输入文件夹路径#输出字典def getCompanyNameFromDir(dir, dir_callbackNone, file_callbackNone):dictAll {}for root, dirs, files in os.walk(dir):for f in files:file_path os.path.join(root, f)if file_callback: file_callback(file_path)dictAll.update(getCompanyName(file_path))return dictAlldef writeDicToFile(dicName, outputFileNamecompany.txt):将字典写入文件中fileOutput open(outputFileName, a)for key, value in dicName.items():strTemp2 value[0]strChina2 strTemp2.decode(unicode_escape)try:fileOutput.write(%-*s % (110, key))fileOutput.write(strChina2.encode(gb2312))except UnicodeEncodeError, e:passfileOutput.write(\n)fileOutput.close()#主函数if __name__ __main__:getCompanyNameFromDir(uD:\\everydaySample\\1221\\10white)print ok finish这里不解释,代码很简单出现的问题如下1. 写入中文str.encode(gb2212)解决2. 出现UnicodeEncodeError 的错误用了try给忽略了希望本文所述对大家的Python程序设计有所帮助。