【发布时间】: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>
任何帮助将不胜感激。
【问题讨论】:
-
看看我的回答,如果有帮助请告诉我