Python中捕获环境异常的困难时间
我有以下简单的代码在特定位置设置日志文件: (Python 2.7.3) 但是,当我尝试运行它时,仍然会出现错误并且无法捕获:Python中捕获环境异常的困难时间,python,python-2.7,Python,Python 2.7,我有以下简单的代码在特定位置设置日志文件: (Python 2.7.3) 但是,当我尝试运行它时,仍然会出现错误并且无法捕获: Traceback (most recent call last): File "trex_daemon_server.py", line 50, in <module> handler = logging.FileHandler("/var/log/trex/trex_daemon_server.log") File "/usr/lib64
Traceback (most recent call last):
File "trex_daemon_server.py", line 50, in <module>
handler = logging.FileHandler("/var/log/trex/trex_daemon_server.log")
File "/usr/lib64/python2.7/logging/__init__.py", line 897, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib64/python2.7/logging/__init__.py", line 916, in _open
stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: '/var/log/trex/trex_daemon_server.log'
回溯(最近一次呼叫最后一次):
文件“trex_daemon_server.py”,第50行,在
handler=logging.FileHandler(“/var/log/trex/trex\u daemon\u server.log”)
文件“/usr/lib64/python2.7/logging/_init__.py”,第897行,在_init中__
StreamHandler.\uuuu init\uuuu(self,self.\u open())
文件“/usr/lib64/python2.7/logging/_init__uuu.py”,第916行,处于打开状态
流=打开(self.baseFilename,self.mode)
IOError:[Errno 13]权限被拒绝:'/var/log/trex/trex\u daemon\u server.log'
奇怪的是,即使是some error
print in也没有打印出来
我在谷歌上搜索了一下,发现这可能是由缩进错误()引起的,但我认为这不是原因
还有什么问题
谢谢,代码似乎在引发
IOError
,而不是EnvironmentError
,基于它应该可以做这项工作,因为IOError和OSError扩展了EnvironmentError。我无法重现这一点。如果我尝试,将引发Environment错误this@cyber101你确定程序停止了吗?也许只是记录。
Traceback (most recent call last):
File "trex_daemon_server.py", line 50, in <module>
handler = logging.FileHandler("/var/log/trex/trex_daemon_server.log")
File "/usr/lib64/python2.7/logging/__init__.py", line 897, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib64/python2.7/logging/__init__.py", line 916, in _open
stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: '/var/log/trex/trex_daemon_server.log'