PermissionError:python中的[Errno 13]

Ben*_*eno 55 python python-3.x

刚刚开始学习一些python,我遇到了如下所述的问题:

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\
Run Code Online (Sandbox Code Playgroud)

似乎是一个文件权限错误,如果任何人可以发光一些,将不胜感激.

注:不知道的Python和Windows文件是如何工作的,但我登录到Windows作为管理员和文件夹具有管理员权限.

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

Joa*_*son 45

When doing;

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

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

Your other example though;

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

如果您只是获得了许可,应该会运作良好a.txt.您可能希望使用原始( - r前缀)字符串,以确保您的路径不包含任何转义\n为特殊字符的转义字符.

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

  • @bennyboy 取决于您要通过打开目录而不是文件来实现什么...? (2认同)

Kai*_*ang 23

对我来说,我正在写入一个在 Excel 中打开的文件。