添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

NOTE: not sure how Python and Windows files work but I'm logged in to Windows as Admin and the folder has admin permissions.

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

@JoachimIsaksson It's definitely a folder, as we can see in the end of the error line. Make that an answer. – Oleh Prypin Nov 3, 2012 at 8:35 Test is a folder, the Document im following reads i can open a text doc using the following syntax a_file = open('E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8') using this syntax aswell as a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8') gives me the same error. – BenniMcBeno Nov 3, 2012 at 8:39 By the way, remember to use raw strings for Windows pathnames, or you'll get bitten badly if a subfolder name should happen to start with n or b or any other letter that can be part of an escape sequence... – Tim Pietzcker Nov 3, 2012 at 8:40 ok so the file was on a flash drive and the flash driver was FAT32 and my HD is NTFS so it was format conflicts >< doing it locally works fine, thanks guys – BenniMcBeno Nov 3, 2012 at 8:46

...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')

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')

For me, I got this error when I was trying to write a file to a folder and wanted to make sure the folder existed. I accidentally used:

path = Path("path/to/my/file.txt")
path.mkdir(parents=True, exist_ok=True)
with open(path, "w") as file:

but the second line means "make a directory at this exact path (and make its parents too, without throwing errors for them existing already)". The third line then throws a PermissionError, because you can't use open() on a directory path, of course! The second line should have been:

path.parent.mkdir(parents=True, exist_ok=True)
				How to search for a keyword through multiple files in a folder & if the word exists replace the word with another word & output the file name
                See more linked questions