Python implements bulk access to vendor information for all files in a specified folder

  • 2020-04-02 14:10:01
  • OfStack

This article illustrates a python implementation for bulk access to vendor information for all files in a specified folder. Share with you for your reference. The details are as follows:

The functional code is as follows:


import os, string, shutil,re 
import pefile 
import codecs, sys 
import wx 
import struct 
# Print on output Unicode character  
#sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout) 
 
def addToDict(theDict,PEfile_Path,strCompanyName): 
  theDict.setdefault(PEfile_Path, [ ]).append(strCompanyName)
  # Existing on the basis of the addition of the list, does not exist on a new dictionary key 
 
def IsPeFile(inputFileName): 
  ''''' Determines whether a file is PE file ''' 
  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  
   
# Gets a file's vendor information  
# Input: file path  
# Output: dictionary  
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) # Written to the file  
  return dictCompany 
 
# Gets the vendor information for all the files in the folder  
# Input: folder path  
# Output: dictionary  
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"): 
  """ Writes the dictionary to a file """ 
  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() 
   
# The main function  
if __name__ == "__main__": 
  getCompanyNameFromDir(u"D:\everydaySample\1221\10white") 
  print "ok finish" 

No explanation here, the code is very simple.

The problems are as follows:

1. Write in Chinese. STR. Encode (' gb2212) solution
2. The error of UnicodeEncodeError occurred, which was ignored by using try

I hope this article has helped you with your Python programming.


Related articles: