简体   繁体   中英

Hard time catching Environment Exception in Python

I have this simple code to setup a logfile at specific location: (Python 2.7.3)

import os, errno
try:
    CCustomLogger.setup_daemon_logger('TRexServer', "/var/log/trex/trex_daemon_server.log")
except EnvironmentError, e:
        print "caught some exception...!"
        if e.errno == errno.EACCES: # catching permission denied error
            print "Launching user must have sudo privileges in order to run T-Rex daemon."
        exit(1)

However, when I try to run it, I still get the error and can't catch it:

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'

What's weird here is that even the some error print in not printed.

I Googled a little bit and found out that this may be caused by indentation error ( see here for example ) but I think this isn't it...

What else could be the problem?

Thanks,

该代码似乎正在引发IOError ,而不是EnvironmentError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM