Python 为什么我会收到 IOError: [Errno 13] Permission denied?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17043814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting IOError: [Errno 13] Permission denied?
提问by
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'
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)
我已经检查了我想要制作日志的特定文件夹中的权限,但仍然收到错误。我的代码是:(名称是 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
My system's file structure is :
我的系统的文件结构是:
/
home
ai
Desktop
home
ubuntu
LCF
ws_code GA-LCF
blackboard.py main-LCF.py
I am calling mainLCF.py from another function lcf.py which is in ws_code .
我正在从 ws_code 中的另一个函数 lcf.py 调用 mainLCF.py 。
采纳答案by b3orn
You need to change the Logfile path by using logging.handlers python module . In my case I did the following stuff :
您需要使用 logging.handlers python 模块更改日志文件路径。就我而言,我做了以下事情:
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
and it worked. However I still don't know the reason why it was earlier trying to make genetic.log at root directory .
它奏效了。但是我仍然不知道为什么它早先尝试在根目录下创建 generic.log 。
回答by b3orn
Looks like logging tried to open the logfile as /genetic.log. If you pass filename as a keyword argument to logging.basicConfigit creates a FileHandlerwhich passes it to os.path.abspathwhich expands the filename to an absolute path based on your current working dir. So you're either in your root dir or your code changes your current working dir.
看起来日志记录试图将日志文件打开为/genetic.log. 如果您将文件名作为关键字参数传递给logging.basicConfig它,则会创建一个FileHandler传递给os.path.abspath它的文件名,该文件名将根据您当前的工作目录将文件名扩展为绝对路径。所以你要么在你的根目录中,要么你的代码改变了你当前的工作目录。
回答by Nitzan Cohen
Although your code seems correct, I think it's better to assign an absolute path. If you develop on your local machine and the app runs on another server, there is probably some differences, like, who calls the process.
Logs are recommended to be written to /var/log/app_name
虽然你的代码看起来是正确的,但我认为最好分配一个绝对路径。如果您在本地机器上开发并且应用程序在另一台服务器上运行,则可能存在一些差异,例如谁调用该进程。
建议写入日志/var/log/app_name

