为什么会出现IOError:[Errno 13]权限被拒绝? [英] Why am I getting IOError: [Errno 13] Permission denied?
问题描述
我正在为代码创建日志文件,但是出现以下错误:
I am creating Log file for the code but I am getting the following error :
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] import mainLCF
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] File "/home/ai/Desktop/home/ubuntu/LCF/GA-LCF/mainLCF.py", line 10, in
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] logging.basicConfig(filename='genetic.log',level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] File "/usr/lib/python2.7/logging/__init__.py", line 1528, in basicConfig
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] hdlr = FileHandler(filename, mode)
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] File "/usr/lib/python2.7/logging/__init__.py", line 901, in __init__
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] StreamHandler.__init__(self, self._open())
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] File "/usr/lib/python2.7/logging/__init__.py", line 924, in _open
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] stream = open(self.baseFilename, self.mode)
[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] IOError: [Errno 13] Permission denied: '/genetic.log'
我已经检查了要创建日志的特定文件夹中的权限,但仍然出现错误. 我的代码是:(名称为mainLCF.py)
I have checked the permissions in the particular folder where I want to make the log but still getting the error . My code is : (name is mainLCF.py)
import logging
import sys
logging.basicConfig(filename='genetic.log',level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.debug("starting of Genetic Algorithm")
sys.path.append("/home/ai/Desktop/home/ubuntu/LCF/ws_code")
import blackboard
from pyevolve import *
def eval_func(chromosome):
some function here
我系统的文件结构是:
/
home
ai
Desktop
home
ubuntu
LCF
ws_code GA-LCF
blackboard.py main-LCF.py
我正在ws_code中的另一个函数lcf.py中调用mainLCF.py.
I am calling mainLCF.py from another function lcf.py which is in ws_code .
推荐答案
您需要通过使用logging.handlers python模块来更改日志文件路径. 就我而言,我做了以下事情:
You need to change the Logfile path by using logging.handlers python module . In my case I did the following stuff :
import logging
from logging.handlers import RotatingFileHandler
import blackboard
WEBAPP_CONSTANTS = {
'LOGFILE': '/home/ai/Desktop/home/ubuntu/LCF/GA-LCF/ga.log',
}
def getWebAppConstants(constant):
return WEBAPP_CONSTANTS.get(constant, False)
LOGFILE = getWebAppConstants('LOGFILE')
log_handler = RotatingFileHandler(LOGFILE, maxBytes=1048576, backupCount=5)
log_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]'))
applogger = logging.getLogger("GA")
applogger.setLevel(logging.DEBUG)
applogger.addHandler(log_handler)
applogger.debug("Starting of Genetic Algorithm")
from pyevolve import *
def eval_func(chromosome):
some function here
,它奏效了.但是我仍然不知道为什么它早些时候试图在根目录下创建generic.log的原因.
and it worked. However I still don't know the reason why it was earlier trying to make genetic.log at root directory .
这篇关于为什么会出现IOError:[Errno 13]权限被拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!