如何往Frappe的服务器写文件
如何写文件, 可以参考如下的写法:
import frappe,uuid
from docxtpl import DocxTemplate
from frappe.utils.file_manager import save_file
from frappe.utils.file_manager import write_file
# bench --site jinxin.local execute erpnextcn.utils.frappe.file.readFile --kwargs '{"file_directory": "public/template", "file_name": "123.txt"}'
@frappe.whitelist()
def readFile(**args):
file_directory = args.get('file_directory')
file_name = args.get('file_name')
# 获取当前 app 的路径
app_path = frappe.get_app_path('erpnextcn')
# 拼凑完整文件路径
file_path = f"{app_path}/{file_directory}/{file_name}"
try:
with open(file_path, 'r') as f:
# 读取文件内容并返回结果
file_content = f.read()
return file_content
except FileNotFoundError:
return "文件不存在"
except Exception as e:
return str(e)
@frappe.whitelist()
def writeFile(**args):
try:
print('开始文件写入')
file_type = args.get('file_type')
file_content = args.get('file_content')
write_path = args.get('write_path')
input_file_name = args.get('input_file_name')
# 获取当前 app 的路径
app_path = frappe.get_app_path('erpnextcn')
# 拼凑完整文件路径
file_path = f"{app_path}/{write_path}/{input_file_name}.{file_type}"
write_file(file_content,file_path)
return "文件写入成功"
except Exception as e:
error_msg = f"文件写入失败: {str(e)}"
print(error_msg) # 输出错误日志
return error_msg