在 Python 中写入文件时权限被拒绝错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18529323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:57:28  来源:igfitidea点击:

Permission denied error while writing to a file in Python

python

提问by user1921843

I want to create a file and write some integer data to it in python. For example, I have a variable abc = 3 and I am trying to write it to a file (which doesn't exist and I assume python will create it on its own):

我想创建一个文件并在 python 中向它写入一些整数数据。例如,我有一个变量 abc = 3,我试图将它写入一个文件(该文件不存在,我假设 python 会自己创建它):

fout = open("newfile.dat", "w")
fout.write(abc)

First, will python create a newfile.dat on its own? Secondly, it's giving me this error:

首先,python 会自己创建一个 newfile.dat 吗?其次,它给了我这个错误:

IOError: [Errno 13] Permission denied: 'newfile.dat'

What's wrong here?

这里有什么问题?

回答by mavili

To answer your first question: yes, if the file is not there Python will create it.

回答你的第一个问题:是的,如果文件不存在,Python 会创建它。

Secondly, the user (yourself) running the python script doesn't have write privileges to create a file in the directory.

其次,运行python脚本的用户(您自己)没有在目录中创建文件的写权限。

回答by Anto

Make sure that you have write permissions for that directory you were trying to create file by properties

确保您对尝试通过属性创建文件的目录具有写权限

回答by Aseem

Please close the file if its still open on your computer, then try running the python code. I hope it works

如果文件在您的计算机上仍然打开,请关闭该文件,然后尝试运行 python 代码。我希望它有效

回答by Derek

This also happens when you attempt to create a file with the same name as a directory:

当您尝试创建与目录同名的文件时,也会发生这种情况:

import os

conflict = 'conflict'

# Create a directory with a given name
try: 
    os.makedirs(conflict)
except OSError:
    if not os.path.isdir(conflict):
        raise

# Attempt to create a file with the same name
file = open(conflict, 'w+')

Result:

结果:

IOError: [Errno 13] Permission denied: 'conflict'

回答by Omar

I've had the same issue using the cmd (windows command line) like this

我在使用 cmd(windows 命令行)时遇到了同样的问题

C:\Windows\System32> "G:\my folder\myProgram.py"

Where inside the python file something like this

在 python 文件中的位置是这样的

myfile = open('myOutput.txt', 'w')

The error was that when you don't use a full path, python would use your current directory, and because the default directory on cmd is

错误是当你不使用完整路径时,python 会使用你当前的目录,因为 cmd 上的默认目录是

C:\Windows\System32 

that won't work, as it seems to be write-protectedand needs permission & confirmation form an administrator

这行不通,因为它似乎被写保护,需要管理员的许可和确认

Instead, you should use full paths, for example:

相反,您应该使用完整路径,例如:

myfile = open('G:\my folder\myOutput.txt', 'w')

回答by Santosh Santu

If you are executing the python script via terminal pass --user to provide admin permissions.

如果您通过终端 pass --user 执行 python 脚本以提供管理员权限。

Worked for me!

为我工作!

If you are using windows run the file as admin.

如果您使用的是 Windows,请以管理员身份运行该文件。

If you are executing via cmd, run cmd as admin and execute the python script.

如果您通过 cmd 执行,请以管理员身份运行 cmd 并执行 python 脚本。

回答by Edwin Rapheal

Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.

权限被拒绝仅表示系统无权将文件写入该文件夹。从终端使用“sudo chmod 777”授予文件夹权限并尝试运行它。它对我有用。

回答by Yatharth Sah

In order to write on a file by using a Python script, you would have to create a text file first. Example A file such as C:/logs/logs.txt should exist. Only then the following code works:

为了使用 Python 脚本写入文件,您必须先创建一个文本文件。示例 应该存在 C:/logs/logs.txt 等文件。只有这样,以下代码才有效:

logfile=open(r"C:/logs/logs.txt",'w')

So summary.

所以总结。

  1. A text file should exist on the specified location
  2. Make sure you close the file before running the Python script.
  1. 指定位置应存在文本文件
  2. 确保在运行 Python 脚本之前关闭文件。

回答by five element god

I write python script with IDLE3.8(python 3.8.0) I have solved this question: if the path is shelve.open('C:\\database.dat')it will be PermissionError: [Errno 13] Permission denied: 'C:\\database.dat.dat'. But when I test to set the path as shelve.open('E:\\database.dat')That is OK!!! Then I test all the drive(such as C,D,F...) on my computer,Only when the Path set in Disk

我写的与我已经解决了这个问题IDLE3.8(蟒蛇3.8.0)python脚本:如果路径是 shelve.open('C:\\database.dat')它会 PermissionError: [Errno 13] Permission denied: 'C:\\database.dat.dat'。但是当我测试将路径设置 shelve.open('E:\\database.dat')为可以时!!!然后我在我的电脑上测试了所有的驱动器(如C、D、F...),只有当路径设置在磁盘

C:\\

C:\\

will get the permission denied error. So I think this is a protect path in windows to avoid python script to change or read files in system Disk(Disk C)

将获得权限被拒绝错误。所以我认为这是Windows中的保护路径,以避免python脚本更改或读取系统磁盘(磁盘C)中的文件