简体   繁体   English

如何在不实际创建文件的情况下从变量或http URL获取文件对象?

[英]How to obtain a file object from a variable or from http URL without actually creating a file?

I want to manipulate a downloaded PDF using PyPDF and for that, I need a file object. 我想使用PyPDF处理下载的PDF,为此,我需要一个文件对象。

I use GAE to host my Python app, so I cannot actually write the file to disk. 我使用GAE托管我的Python应用程序,因此实际上无法将文件写入磁盘。

Is there any way to obtain the file object from URL or from a variable that contains the file contents? 有什么方法可以从URL或包含文件内容的变量中获取文件对象?

TIA. TIA。

大多数工具(包括urllib )已经为您提供了类似文件的功能,但是如果您需要真正的随机访问,则需要创建一个StringIO.StringIO并将数据读入其中。

In GAE you can use the blobstore to read, write file data and to upload and download files. 在GAE中,您可以使用Blobstore读取,写入文件数据以及上传和下载文件。 And you can use the File API: 您可以使用File API:

Example : 范例:

_file = files.blobstore.create(mime_type=mimetype, _blobinfo_uploaded_filename='test')
with files.open(_file, 'a') as f :                                                      
    f.write(somedata)                                                         
files.finalize(_file)

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

相关问题 Python Django:在内存中创建文件对象,而无需实际创建文件 - Python Django : Creating file object in memory without actually creating a file 从url检索数据而不创建文件 - Retrieve data from url without creating a file 从php url获取可下载二进制文件的文件名,而无需实际下载文件 - Get filename of downloadable binary from php url without actually downloading the file 如何从变量设置创建文件的名称? - How to set a name of creating file from variable? Python - 获取统计信息而不从输入文件创建变量/数据帧 - Python - getting statistics without creating variable/dataframe from input file Python:如何在不知道文件实际存在多长时间的情况下从文件中读取一大块文本? - Python: How do you read a chunk of text from a file without knowing how long the file actually is? urllib2什么时候实际从网址下载文件? - When does urllib2 actually download a file from a url? 如何从XML和XSLT获取HTML文件 - How to obtain HTML file from XML and XSLT 如何使子流程对象将输出保存到临时文件,然后从临时文件获取值? - How to make a subprocess object save output to a temporary file and then obtain values from temporary file? 从模块中获取类而无需实际运行文件 - Fetching classes from a module without actually having to run the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM