Python IOError的原因:[Errno 13]权限被拒绝 [英] causes of Python IOError: [Errno 13] Permission denied

查看:408
本文介绍了Python IOError的原因:[Errno 13]权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试写入文件时,如果满足以下任一条件,我都会遇到相同的错误:

When attempting to write a file, I can get this same error when any of following conditions applies:

  1. 该文件已存在,并被标记为只读.

  1. The file exists and is marked read-only.

我没有该文件夹的写入权限,因此无法创建该文件夹 文件.

I don't have write permission for the folder and therefore cannot create a file.

该文件已经在另一个进程中打开.

The file is already open in another process.

有什么办法可以区分出上述三个原因?如果没有的话,我会 说这是一个非常糟糕的设计.

Is there any way to separate out the above three causes? If not, then I would say that this is an extraordinarily poor design.

推荐答案

您得到的Exception相同,因为基本问题是您不能这样做",但详细信息包含在异常实例中,(或较旧的python上的字符串),因此如果您将问题处理为:

You get the same Exception as your base problem is "You can't do this" but the details are contained in the exception instance, (or in a string on older pythons), so if you handle the problem as:

try:
    outfile = open('somename.txt', 'w')
except IOError, e:
    print "Not allowed", e

您将获得更多信息. (注:如果您运行的是python 3,则需要在上面的打印内容中添加方括号).

You will get a lot more information. (N.B. if you are running python 3 you will need to add brackets to the print above).

这实际上是一个非常好的设计-您的基本问题是存在问题-如果您愿意的话可以找到更多信息-并且可以以通用的方式处理给定类别的问题.

This is actually incredibly good design - your basic problem is that there is a problem - and you can find out more if you care to - and you can handle a given class of problem in a generalised manner.

这篇关于Python IOError的原因:[Errno 13]权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆