python实现批量获取指定文件夹下的所有文件的厂商信息

yipeiwu_com5年前Python基础

本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法。分享给大家供大家参考。具体如下:

功能代码如下:

import os, string, shutil,re 
import pefile 
import codecs, sys 
import wx 
import struct 
#输出中打印Unicode字符 
#sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout) 
 
def addToDict(theDict,PEfile_Path,strCompanyName): 
  theDict.setdefault(PEfile_Path, [ ]).append(strCompanyName)
  #存在就在基础上加入列表,不存在就新建个字典key 
 
def 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 1 
    else: 
      return 0 
  else: 
    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_callback=None, file_callback=None): 
  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 dictAll 
 
def writeDicToFile(dicName, outputFileName="company.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: 
    pass 
    fileOutput.write("\n") 
   
  fileOutput.close() 
   
#主函数 
if __name__ == "__main__": 
  getCompanyNameFromDir(u"D:\\everydaySample\\1221\\10white") 
  print "ok finish" 

这里不解释,代码很简单.

出现的问题如下:

1. 写入中文.str.encode('gb2212')解决
2. 出现UnicodeEncodeError 的错误,用了try给忽略了

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python实现简单生成验证码功能【基于random模块】

本文实例讲述了Python实现简单生成验证码功能。分享给大家供大家参考,具体如下: 验证码一般用来验证登陆、交易等行为,减少对端为机器操作的概率,python中可以使用random模块,...

Python Trie树实现字典排序

Python Trie树实现字典排序

一般语言都提供了按字典排序的API,比如跟微信公众平台对接时就需要用到字典排序。按字典排序有很多种算法,最容易想到的就是字符串搜索的方式,但这种方式实现起来很麻烦,性能也不太好。Trie...

Python字典操作简明总结

1.dict()创建字典 复制代码 代码如下: >>> fdict = dict((['x', 1], ['y', 2])) >>> fdict {'...

详解Django-auth-ldap 配置方法

使用场景 公司内部使用Django作为后端服务框架的Web服务,当需要使用公司内部搭建的Ldap 或者 Windows 的AD服务器作为Web登录认证系统时,就需要这个Django-au...

Python 使用Numpy对矩阵进行转置的方法

Python 使用Numpy对矩阵进行转置的方法

如下所示: matrix.py #!/usr/bin/python # -*- encoding:UTF-8-*- import pprint import numpy as np...