[英]Python - how to open a file that is not yet written to disk?
I am using a script to strip exif data from uploaded JPGs in Python, before writing them to disk. 我使用脚本从Python中上传的JPG中剥离exif数据,然后再将其写入磁盘。 I'm using Flask, and the file is brought in through requests
我正在使用Flask,并且文件是通过请求引入的
file = request.files['file']
strip the exif data, and then save it 剥离exif数据,然后将其保存
f = open(file)
image = f.read()
f.close()
outputimage = stripExif(image)
f = ('output.jpg', 'w')
f.write(outputimage)
f.close()
f.save(os.path.join(app.config['IMAGE_FOLDER'], filename))
Open isn't working because it only takes a string as an argument, and if I try to just set f=file
, it throws an error about tuple objects not having a write attribute. Open不起作用,因为它只接受一个字符串作为参数,并且如果我尝试仅设置
f=file
,它将引发有关不具有write属性的元组对象的错误。 How can I pass the current file into this function before it is read? 如何在读取之前将当前文件传递给此函数?
file
is a FileStorage
, described in http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage file
是FileStorage
,在http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage中进行了描述
As the doc says, stream
represents the stream of data for this file, usually under the form of a pointer to a temporary file, and most function are proxied. 正如文档所说,
stream
表示该文件的数据流,通常以指向临时文件的指针的形式出现,并且大多数功能都被代理。
You probably can do something like: 您可能可以执行以下操作:
file = request.files['file']
image = file.read()
outputimage = stripExif(image)
f = open(os.path.join(app.config['IMAGE_FOLDER'], 'output.jpg'), 'w')
f.write(outputimage)
f.close()
Try the io package, which has a BufferedReader(), ala: 尝试io包,它具有BufferedReader()ala:
import io
f = io.BufferedReader(request.files['file'])
...
file = request.files['file']
image = stripExif(file.read())
file.close()
filename = 'whatever' # maybe you want to use request.files['file'].filename
dest_path = os.path.join(app.config['IMAGE_FOLDER'], filename)
with open(dest_path, 'wb') as f:
f.write(image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.