简体   繁体   English

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

[英]Read a file without saving it locally python/bottle

Hi so basically I have a form that uploads a file嗨所以基本上我有一个上传文件的表格

 <input class="btn btn-light" type="file" id="file" name="h" accept=".py">

I'm using bottle, so essentially I want to get the contents of the file but not have to save it locally.我正在使用瓶子,所以基本上我想获取文件的内容,但不必将其保存在本地。 Using with open requires a path which I don't have as it is not saved locally.使用 with open 需要一个我没有的路径,因为它没有保存在本地。

f = request.POST['file']

I get the file using the above.我使用上面的方法获取文件。

Is there any way I could upload the file without having to save it locally?有什么办法可以上传文件而不必在本地保存?

Any help would be much appreciated, I just need the name of the file and the contents.任何帮助将不胜感激,我只需要文件名和内容。

No need to save the file (or to use BytesIO as suggested in a comment).无需保存文件(或按照评论中的建议使用 BytesIO)。 You can simply read its contents:您可以简单地阅读其内容:

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()

Output: Output:

$ 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python Firebase Cloud Storage - 上传文件而不将其保存在本地 - python Firebase Cloud Storage - upload file without saving it locally Python - 读取音频数据而不保存到文件 - Python - Read Audio Data Without Saving to File Python Bottle上传文件(不带表单) - Python Bottle upload file without form 创建一个文本文件,并通过电子邮件发送而不将其保存在本地 - Create a text file, and email it without saving it locally 保存内容 <textarea> 用瓶子归档 - Saving the content of a <textarea> to file with Bottle Python Bottle文件上传 - Python Bottle File Upload 在没有HTML表单的情况下将文件上传到Python瓶服务器 - Uploading a file to Python bottle server without HTML form 有没有一种方法可以从电子邮件附件中获取 excel 文件,并在 python 脚本中提取所有数据,而无需在本地保存文件? - Is there a way I can get an excel file from an email attachment and extract it’s data all within a python script without saving the file locally? 从请求将文件上传到云存储而不在本地保存 - Upload file to cloud storage from request without saving it locally 将.txt 文件上传到 Dropbox,而不先在本地保存 - Upload .txt file to Dropbox without saving locally first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM