Python IOError: [Errno 13] 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29331872/
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 04:25:14  来源:igfitidea点击:

IOError: [Errno 13] Permission denied

python

提问by Jo?o Pedro

I have this piece of code to create a .json file to store python data. When i run it in my server i get this error:

我有这段代码来创建一个 .json 文件来存储 python 数据。当我在我的服务器中运行它时,我收到此错误:

IOError: [Errno 13] Permission denied: 'juliodantas2015.json' at line with open(output_file, 'wb') as fp:

Python code:

蟒蛇代码:

fich_input='juliodantas2015.txt'
output_file= fich_input.strip('.txt')+'.json'
import json
with open(output_file, 'wb') as fp:
    json.dump('yes', fp)

In command line i typed chmod 777 *.pybut still not working. How can i fix this ?

在命令行中,我输入了chmod 777 *.py但仍然无法正常工作。我怎样才能解决这个问题 ?

采纳答案by Marcus Müller

IOError: [Errno 13] Permission denied: 'juliodantas2015.json'

tells you everything you need to know: though you successfully made your python program executable with your chmod, python can't open that juliodantas2015.json'file for writing. You probably don't have the rights to create new files in the folder you're currently in.

告诉你,你需要知道的一切:虽然你成功地与您取得你的Python程序可执行文件chmod,蟒蛇无法打开juliodantas2015.json'文件进行写入。您可能无权在当前所在的文件夹中创建新文件。

回答by Mo Jo

I had a similar problem. I was attempting to have a file written every time a user visits a website.

我有一个类似的问题。每次用户访问网站时,我都试图编写一个文件。

The problem ended up being twofold.

问题最终是双重的。

1: the permissions were not set correctly

1:权限设置不正确

2: I attempted to use
f = open(r"newfile.txt","w+")(Wrong)

2:我尝试使用
f = open(r"newfile.txt","w+")(错误)

After changing the file to 777 (all users can read/write)
chmod 777 /var/www/path/to/file
and changing the path to an absolute path, my problem was solved
f = open(r"/var/www/path/to/file/newfile.txt","w+")(Right)

把文件改成777(所有用户都可以读/写)
chmod 777 /var/www/path/to/file
,把路径改成绝对路径后,我的问题就解决了
f = open(r"/var/www/path/to/file/newfile.txt","w+")(右)

回答by bonjonbovi

I have a really stupid use case for why I got this error. Originally I was printing my data > file.txt

我有一个非常愚蠢的用例来说明为什么会出现此错误。最初我正在打印我的数据 > file.txt

Then I changed my mind, and decided to use open("file.txt", "w") instead. But when I called python, I left > file.txt .....

然后我改变了主意,决定使用 open("file.txt", "w") 代替。但是当我调用python时,我离开了> file.txt .....