【问题标题】:Errno.13 Permission Denied while trying to access files through HTML and FlaskErrno.13 尝试通过 HTML 和 Flask 访问文件时权限被拒绝
【发布时间】:2020-07-24 14:53:45
【问题描述】:

所以,这几天我一直在尝试学习 Flask,最近我尝试制作一个简单的应用程序,可以让您上传文本文件并读取文本文件的内容。我能够成功上传文件,但是当我尝试读取文件时,出现此错误:

PermissionError: [Errno 13] Permission denied: 'txt1.txt'

(我试图读取一个名为 txt1.txt 的文件) 这是我的 Python 代码:

from flask import Flask,request,render_template,redirect,url_for
from werkzeug import secure_filename

app=Flask(__name__)

@app.route("/",methods=["POST","GET","DELETE"])
def home():
    return render_template("ftest.html")
@app.route("/success",methods=["POST","GET","DELETE"])
def success():
if request.method=="POST":
    if request.files:
        f=request.files[ "file"]
        f.save(secure_filename(f.filename))
        print(f)
        rd=f.read()
        print(rd)
return "Hi"

if __name__=="__main__":
   app.run(debug=True)
   app.run()

这里是 ftest.html:

<html>
<style>
body {
background-color: #8a1e1e ;
}
h1 {
color: white;
text-align: center;
font-family: impact;
font-size: 100px
}
form{
height: 22px;
width: 316px;
background-color: #4C83DC
}
</style>
<head>
<title>Txt Uploader</title> 
</head>
<body>
<h1>Txt Uploader</h1>
<form action = "/success" method = "post" enctype="multipart/form-data">
  <input type="file" name="file" />
  <input type = "submit" value="Upload">

  </form>
  </body>
  </html>

任何帮助将不胜感激。

【问题讨论】:

  • 看看我的回答,如果有帮助请告诉我

标签: python html forms flask


【解决方案1】:

这里有一些可能的解决方案

  1. 更改要保存的目录的权限,以便 所有用户都有读写权限。
  2. 确保首先关闭您尝试写入的文件。只需添加rd.close()
  3. 如果您使用的是 Pycharm。右键单击您的 PyCharm 应用程序,然后 以管理员身份运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2017-08-22
    • 2019-04-05
    • 1970-01-01
    • 2018-11-10
    • 2021-09-05
    相关资源
    最近更新 更多