权限错误:Python 中的 [Errno 13] [英] PermissionError: [Errno 13] in Python

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

问题描述

刚开始学习一些 Python,我遇到了如下问题:

Just starting to learn some Python and I'm having an issue as stated below:

a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
PermissionError: [Errno 13] Permission denied: 'E:\\Python Win7-64-AMD 3.3\\Test\

似乎是一个文件权限错误,如果有人能给我一些启发,将不胜感激.

Seems to be a file permission error, if any one can shine some light it would be greatly appreciated.

注意:不确定 Python 和 Windows 文件如何工作,但我以管理员身份登录到 Windows,并且该文件夹具有管理员权限.

我已尝试将 .exe 属性更改为以管理员身份运行.

I have tried changing .exe properties to run as Admin.

推荐答案

什么时候做;

a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')

...您正在尝试将目录作为文件打开,这可能(并且在大多数非 UNIX 文件系统上)会失败.

...you're trying to open a directory as a file, which may (and on most non UNIX file systems will) fail.

你的另一个例子;

a_file = open('E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')

如果您只拥有 a.txt 的权限,应该可以很好地工作.不过,您可能希望使用原始 (r-prefixed) 字符串,以确保您的路径不包含任何转义字符,如 \n 将被转换为特殊字符.

should work well if you just have the permission on a.txt. You may want to use a raw (r-prefixed) string though, to make sure your path does not contain any escape characters like \n that will be translated to special characters.

a_file = open(r'E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')

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

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