python常用(1)
- # encoding: utf-8
- from openpyxl.reader.excel import load_workbook # FOR xlsx
- import os
- import openpyxl
- from decimal import Decimal
- import shutil
- import zipfile
- path = os.getcwd()
- testplan = path + "\\testplan.xlsx"
- mapping_path = os.path.join(path, "MUT_Product_Mapping.xlsx")
- #print(mapping_path)
- workbook = load_workbook(mapping_path)
- worksheet = workbook.worksheets[0]
- mapping = {}
- for index, item in enumerate(worksheet.rows):
- # START with age row
- if(index < 1):
- continue
- #print(item[3].value)
- #if item[3].value not in mapping:
- # mapping[item[3].value] = []
- mapping[item[0].value]=item[3].value
- output_path =path+"\\output\\result\\"
- output_list = os.listdir(output_path)
- #remmove old files
- target_dirs = os.listdir(path)
- for dir in target_dirs:
- if dir.startswith('HKG_'):
- #print(dir)
- input = path+'\\'+dir+'\\TCs\\input'
- output = path+'\\'+dir+'\\TCs\\output'
- if os.path.exists(input):
- shutil.rmtree(input)
- os.mkdir(input)
- shutil.copy(testplan, input)
- if os.path.exists(output):
- shutil.rmtree(output)
- os.mkdir(output)
- #update input.json
- f = open(path+'\\input.json')
- content = f.read()
- newcontent = content.replace('{product_id}', dir).replace('{product_code}', dir[4::])
- with open(path+'\\'+dir+'\\input.json', 'w') as i:
- i.write(newcontent)
- #copy new files
- for filename in output_list:
- file_path = output_path+"\\"+filename
- if(not filename.endswith('.xml')):
- continue
- print(file_path)
- f = open(file_path, 'r')
- content = f.read()
- key = ''
- plancode = content.split('<ProductId>')[1].split('</ProductId>')[0]
- tracheid = content.split('<TrancheId>')[1].split('</TrancheId>')[0]
- if tracheid == '--':
- key = plancode
- else:
- key =plancode+','+tracheid
- target = ''
- if key in mapping:
- target = mapping[key]
- else:
- print(key+' not exists')
- print(target)
- output = path +'\\HKG_'+target+'\\TCs\\output\\'+filename
- #print(output)
- shutil.copy(file_path, output)
- #break
- # 递归目录
- def dfs_get_zip_file(input_path, result, ignore=[]):
- files = os.listdir(input_path)
- for file in files:
- filePath = input_path + '/' + file
- if file in ignore:
- print("ignored: " + filePath)
- continue
- if os.path.isdir(filePath):
- dfs_get_zip_file(filePath, result, ignore)
- else:
- print("added: " + filePath)
- result.append(filePath)
- def zip_path(input_path, output_path, ignore=[]):
- outdir = os.path.dirname(output_path)
- if not os.path.isdir(outdir):
- os.makedirs(outdir)
- f = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
- filelists = []
- dfs_get_zip_file(input_path, filelists, ignore)
- print('start compress file...')
- for file in filelists:
- print('compress: ' + file)
- file = file.replace('\\', '/')
- input_path = input_path.replace('\\', '/')
- f.write(file, file.replace(input_path, ''))
- f.close()
- #package to zip
- for dir in target_dirs:
- if dir.startswith('HKG_'):
- print('zip:'+dir)
- input_path = path +'\\'+ dir +'\\'
- output_path= path +'\\'+ dir +'\\'+dir+'.zip'
- if os.path.exists(output_path):
- os.remove(output_path)
- zip_path(input_path, output_path, ignore=[dir+'.zip'])
- #break
- print('completed.')
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · python静态变量赋值
- · 可在阿里云dataworks直接使用的worldbank data api
- · [转]用python爬取指定用户微博图片及内容,并进行微博分类及使用习惯分析
- · python批量修改文件名
- · python 怎么添加中文注释
- · python删除redis所有数据
- · python使用redis示例
- · python 四元数 转 欧拉角
- · 曲线(轨迹)相似度算法——LCSS最长公共子序列算法
- · python版DTW动态时间规划算法
- · macOS Charles 4.x版本的安装及使用(含破解激活)
- · centos安装chrome+chromedriver