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程序设计有所帮助。

相关文章

Linux下python3.6.1环境配置教程

Linux下python3.6.1环境配置教程

linux系统环境自带python2.6,但有时我们项目使用的版本可能是3.x以上等等,此时我们需要在linux中再安装项目所需的python版本,此时就涉及多版本共存问题了,很多同学在...

python英语单词测试小程序代码实例

这篇文章主要介绍了python英语单词测试小程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 爬取了扇贝英语网,并制作了一个...

Python实现批量将word转html并将html内容发布至网站的方法

本文实例讲述了Python实现批量将word转html并将html内容发布至网站的方法。分享给大家供大家参考。具体实现方法如下: #coding=utf-8 __author__ =...

Python编程中的异常处理教程

1、异常简介 从软件方面来说,错误是语法或是逻辑上的,当python检测到一个错误时,解释器就会指出当前流已经无法继续执行下去,这时候就出现了异常。异常分为两个阶段:首先是引起异常发生的...

Fabric 应用案例

示例1:文件打包,上传与校验 我们时常做一些文件包分发的工作,实施步骤一般是先压缩打包,在批量上传至目标服务器,最后做一致性校验,本案例通过put()方法实现文件的上传,通过对比本地与远...