项目需要,首先需要读取json格式存储的文本文档,该文本文档存在指定的目录中,该目录下有很多的文本文档,需要在文本文档中找到里面的calls数据,然后把calls数据返回,供robotframework 调用,然后在另一个测试用例中将calls数据和时间间隔写入另一个文本文档中。
#-*- coding:utf-8 -*-
import os
class Write_in_Txt(object):
def __init__(self):
self._write_in_file = None
def get_written_data(self, calls1, calls2):
dict_call = {"calls_before" :calls1, "calls_after" :calls2}
return dict_call
def write_to_txt_file(self, file_path, dict_call, time_interval):
if not os.path.exists(file_path):
file_name = open(file_path, 'w')
for key,value in dict_call.items():
print 'key',key
print 'value',value
file_name.write(key + ":" + value + '---')
file_name.write('time_interval:' + time_interval)
file_name.write('\n')
file_name.close()
else:
file_name = open(file_path, 'a')
for key,value in dict_call.items():
print 'key',key
print 'value',value
file_name.write(key + ":" + value + '---')
file_name.write('time_interval:' + time_interval)
file_name.write('\n')
file_name.close()
if __name__ == '__main__':
current_dir = os.getcwd()
print 'current_dir', current_dir
file_path = current_dir +"\\" + "\\calls_collection.txt"
write_in_file = Write_in_Txt()
dict_data = write_in_file.get_written_data("10","11")
write_in_file.write_to_txt_file(file_path, dict_data, "2016-08-01~2016-08-10")
dict_data2 = write_in_file.get_written_data("5","6")
write_in_file.write_to_txt_file(file_path, dict_data, "2016-05-01~2016-05-10")