PyLint在的错误处理程序函数中引发“misplacedbareraise”舒蒂尔.rmtree(...)

2024-09-30 10:42:41 发布

您现在位置:Python中文网/ 问答频道 /正文

上下文: 我正在使用^{}(delDir, ignore_errors = False, onerror = readOnlyErrorHandler)删除包含只读文件的目录树:

烦恼:PyLint(在VS代码中)将我的readOnlyErrorHandler函数中的raise命令标记为

  • “raise语句不在except子句中”,pylint(misplaced bare raise)

问题:是否有办法在不禁用整个文件的linting的情况下获取此警告?在

def readOnlyErrorHandler(func, path, exc_info):
  import errno
  if func in (os.rmdir, os.unlink, os.remove) and exc_info[1].errno == errno.EACCES:
    print (f"Retry '{func.__name__}' after chmod 0o777 on '{path}'")
    os.chmod(path, 0o777) 
    func(path)
  else:
    # marked as 'The raise statement is not inside an except clause' 
    # by pylint(misplaced-bare-raise)
    raise  # purpose: rethrow the other errors that brought me here

系统:Windows,Python 3.6.3

测试方法:

^{pr2}$

Tags: 文件pathinfoospylintexcraisefunc
1条回答
网友
1楼 · 发布于 2024-09-30 10:42:41

您已经掌握了^{}中发生的异常的所有信息。在本例中,exc_info将类似于

(
    <class 'FileNotFoundError'>, 
    FileNotFoundError(2, 'No such file or directory'), 
    <traceback object at 0x7fae2a66a0c8>
)

所以你可以用

^{2}$

或自定义错误消息(但保留异常类型):

raise exc_info[0]("exception from readOnlyErrorHandler")

相关问题 更多 >

    热门问题