将软件布置在第三方电脑上会出现无法提前指定绝对路径的情况,这回影响到后续的文件读写;json文件是数据交换的一种基本方法,为了减少重复造轮子,经行标准化代码。
关于路径:
import os
workspace=os.getcwd()
path=os.path.join(workspace,'calibration.json')
print(path)
由此得到calibration.json的文件绝对路径,便于后续读写
关于json读写:
Dict1={"PPfilDifnThdDual_C":-0.5000000000000000,"PFltSig_ratiag_C":0.300450000000000000,"PFltSig_1_T":{"ST/X":[-150.0,85.6,221.3],"WERT":[0.03650,0.0360,0.02629]}}
Dict2={"PPfilDifnThdDual_C":-0.5000000000000000,"PFltSig_ratiag_C":0.300450000000000000,"PFltSig_1_T":{"ST/X":[-150.0,85.6,221.3],"WERT":[0.03650,0.0360,0.02629]}}dict3={}//新建一个字典,通过以下两行将上述字典合并成一个dict3.update(dict1)dict3.update(dict2)
with open(path,"w") as f:#写入calibrationjson.dump(dict3,f)
注:如果写两个dump会出错,导致读取json失败,验证写入的json是否正确可以用网上的在线工具检测。
读取json:
with open(path, 'r', encoding='UTF-8') as f:#读取calibration# print(f.read())str_=f.read()print(type(str_))//字符串dict_ = json.loads(str_)//字典print(dict_)
在flask中调用subprocess运行bat文件可能出现的问题是线程从flask中跳转不回来,原因可能是subprocess线程没有做完就运行了下面的代码,比如退出本路由地址。
可能的解决方案是:
p=subprocess.Popen("start.bat", shell=False)stdout,stderr =p.communicate()p.wait()
这个方法不知道什么原理,但是目前能管用挡一下。
转载于:https://blog.51cto.com/14156081/2337976