【发布时间】:2018-11-28 16:51:31
【问题描述】:
我有一种记录 json 文件操作的方法:
def log(self, action, data):
import json
import os
current_directory = os.path.dirname(os.path.realpath(__file__))
try:
with open(current_directory+'/logs/log_'+str(data['id'])+'.json', 'a+') as outfile:
log_data = {
str(datetime.today()): {
'action': action,
'data': data
}
}
json.dump(log_data, outfile, indent=2)
except Exception as e:
print('\033[91m'+"Couldn't access log file to log changes in Reservation model: [{}]".format(str(e))+'\033[0m')
print('Current directory is {}'.format(os.path.dirname(os.path.realpath(__file__))))
它在我的 Mac 上本地工作,但它总是在 Linux 服务器中返回 permission denied。 logs 目录权限设置为755 和chmod,但我仍然遇到同样的错误:
[Wed Nov 28 16:27:23.551207 2018] [wsgi:error] [pid 18955:tid 139905951835904] [remote 189.149.229.31:63243] \x1b[91mCouldn't access log file to log changes in Reservation model: [[Errno 13] Permission denied: '/home/axel/IntellibookProject/Intellibook/ReservationsManagerApp/logs/log_43.json']\x1b[0m
[Wed Nov 28 16:27:23.551346 2018] [wsgi:error] [pid 18955:tid 139905951835904] [remote 189.149.229.31:63243] Current directory is /home/axel/IntellibookProject/Intellibook/ReservationsManagerApp
【问题讨论】:
-
logs目录是否存在?如果不创建它。 -
是的,确实如此。如果没有,我会得到一个不同的错误代码
-
日志目录的权限呢?你能在里面手动创建一个文件吗?
-
@Jean-François Fabre 是的,我可以,
-
为什么是“a+”模式?试试“a”。并在写入文件之前检查文件是否不存在。可能文件不能被覆盖
标签: python django linux apache