分类目录

链接

2021 年 7 月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Python > 正文
python常用(1)
Python 暂无评论 阅读(122)
  1. # encoding: utf-8
  2. from openpyxl.reader.excel import load_workbook  # FOR xlsx
  3. import os
  4. import openpyxl
  5. from decimal import Decimal
  6. import shutil
  7. import zipfile
  8.  
  9.  
  10. path = os.getcwd()
  11.  
  12. testplan = path + "\\testplan.xlsx"
  13.  
  14. mapping_path = os.path.join(path, "MUT_Product_Mapping.xlsx")
  15. #print(mapping_path)
  16. workbook = load_workbook(mapping_path)
  17. worksheet = workbook.worksheets[0]
  18.  
  19. mapping = {}
  20.  
  21. for index, item in enumerate(worksheet.rows):
  22.     
  23.     # START with  age row
  24.     if(index < 1):
  25.         continue
  26.     #print(item[3].value)
  27.     #if item[3].value not in mapping:
  28.     #    mapping[item[3].value] = []
  29.  
  30.     mapping[item[0].value]=item[3].value
  31.  
  32.     
  33.  
  34. output_path =path+"\\output\\result\\"
  35. output_list = os.listdir(output_path)
  36.  
  37. #remmove old files
  38. target_dirs = os.listdir(path)
  39. for dir in target_dirs:
  40.     if dir.startswith('HKG_'):
  41.         #print(dir)
  42.         input = path+'\\'+dir+'\\TCs\\input'
  43.         output = path+'\\'+dir+'\\TCs\\output'
  44.         if os.path.exists(input):
  45.             shutil.rmtree(input)
  46.         os.mkdir(input)
  47.         shutil.copy(testplan, input)
  48.             
  49.         if os.path.exists(output):
  50.             shutil.rmtree(output)
  51.         os.mkdir(output)    
  52.  
  53.         #update input.json
  54.         f = open(path+'\\input.json')
  55.         content = f.read()
  56.         newcontent = content.replace('{product_id}', dir).replace('{product_code}', dir[4::])
  57.         with open(path+'\\'+dir+'\\input.json', 'w') as i:
  58.             i.write(newcontent)
  59.  
  60.  
  61. #copy new files
  62. for filename in output_list:
  63.     file_path = output_path+"\\"+filename
  64.     if(not filename.endswith('.xml')):
  65.         continue
  66.     print(file_path)
  67.     f = open(file_path, 'r')
  68.     content = f.read()
  69.     
  70.     key = ''
  71.     plancode = content.split('<ProductId>')[1].split('</ProductId>')[0]
  72.     tracheid = content.split('<TrancheId>')[1].split('</TrancheId>')[0]
  73.     if tracheid == '--':
  74.         key = plancode
  75.     else:
  76.         key =plancode+','+tracheid
  77.  
  78.     target = ''
  79.     if key in mapping:
  80.         target = mapping[key]
  81.     else:
  82.         print(key+' not exists')    
  83.     print(target)
  84.  
  85.  
  86.     output = path +'\\HKG_'+target+'\\TCs\\output\\'+filename
  87.     #print(output)    
  88.     shutil.copy(file_path, output)
  89.     
  90.     #break
  91.  
  92. # 递归目录
  93. def dfs_get_zip_file(input_path, result, ignore=[]):
  94.     files = os.listdir(input_path)
  95.     for file in files:
  96.         filePath = input_path + '/' + file
  97.         if file in ignore:
  98.             print("ignored: " + filePath)
  99.             continue
  100.         if os.path.isdir(filePath):
  101.             dfs_get_zip_file(filePath, result, ignore)
  102.         else:
  103.             print("added: " + filePath)
  104.             result.append(filePath)
  105.  
  106.  
  107. def zip_path(input_path, output_path, ignore=[]):
  108.     outdir = os.path.dirname(output_path)
  109.     if not os.path.isdir(outdir):
  110.         os.makedirs(outdir)
  111.     f = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
  112.     filelists = []
  113.     dfs_get_zip_file(input_path, filelists, ignore)
  114.     print('start compress file...')
  115.     for file in filelists:
  116.         print('compress: ' + file)
  117.         file = file.replace('\\', '/')
  118.         input_path = input_path.replace('\\', '/')
  119.         f.write(file, file.replace(input_path, ''))
  120.     f.close()
  121.  
  122. #package to zip
  123. for dir in target_dirs:
  124.     if dir.startswith('HKG_'):
  125.         print('zip:'+dir)
  126.         input_path = path +'\\'+ dir +'\\'
  127.         output_path= path +'\\'+ dir +'\\'+dir+'.zip'
  128.         if os.path.exists(output_path):
  129.             os.remove(output_path)
  130.         zip_path(input_path, output_path, ignore=[dir+'.zip'])
  131.         #break
  132.  
  133.  
  134. print('completed.')

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:python常用(1) | Bruce's Blog

发表评论

留言无头像?