背景:需要写入文件,传入文件名称,已经写入的目标文件,返回文件路径
import json
import os
class File:
@staticmethod
def write_file(filename, write_data):
"""
写入文件,先判断文件路径是否存在,如果存在,先删除文件,然后进行插入操作
"""
file_path = os.getcwd() + '/' + filename
if os.path.exists(file_path):
print('当前文件已经存在,删除之后,在进行插入')
os.remove(file_path)
write_data = json.dumps(write_data)
with open(file_path, "a") as fr:
fr.writelines(write_data + "\n")
else:
print('no such file:%s' % file_path) # 则返回文件不存在
return file_path
版权声明:本文为xxlovesht原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。