读取文件而不将其保存在本地python/Battle

读取文件而不将其保存在本地python/Battle,python,bottle,Python,Bottle,嗨,基本上我有一个上传文件的表单 <input class="btn btn-light" type="file" id="file" name="h" accept=".py"> 我使用上述方法获取文件 我有没有办法上传文件而不必在本地保存 任何帮助都将不胜感激,我只需要文件名和内容。无需保存文件(或使用评论中建议的BytesIO)。您只需阅读其内容: from bottle i

嗨,基本上我有一个上传文件的表单

 <input class="btn btn-light" type="file" id="file" name="h" accept=".py">
我使用上述方法获取文件

我有没有办法上传文件而不必在本地保存

任何帮助都将不胜感激,我只需要文件名和内容。

无需保存文件(或使用评论中建议的BytesIO)。您只需阅读其内容:

from bottle import Bottle, request

app = Bottle()

@app.post("/upload")
def upload():
    f = request.POST["file_name"]
    text = f.file.read()
    return f"Read {len(text)} bytes from the uploaded file.\n"

app.run()
输出:

$ echo "This is the file to upload." > file.txt
$ curl http://127.0.0.1:8080/upload -F file_name=@file.txt
Read 28 bytes from the uploaded file.

您正在寻找
io.BytesIO
$ echo "This is the file to upload." > file.txt
$ curl http://127.0.0.1:8080/upload -F file_name=@file.txt
Read 28 bytes from the uploaded file.