0

I want to manipulate a downloaded PDF using PyPDF and for that, I need a file object.

I use GAE to host my Python app, so I cannot actually write the file to disk.

Is there any way to obtain the file object from URL or from a variable that contains the file contents?

TIA.

Alex Bausk
  • 690
  • 5
  • 29

2 Answers2

3

Most tools (including urllib) already give you a file-like, but if you need true random access then you'll need to create a StringIO.StringIO and read the data into it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Now I see you answer is the best on what he is asking. – Jimmy Kane Dec 19 '12 at 15:26
  • Thanks for the insights. I am now stuck trying to un-botch the file that I am receiving from a Javascript frontend, but that's another story. I will folow up and mark the answers as soon as I get to test the both options with PyPDF and an actual file. – Alex Bausk Dec 20 '12 at 08:57
  • Well, from what I've learned about StringIO to date, it's a really good way to go with handling files in my context. I will redesign my code to use StringIO. Thanks a lot! – Alex Bausk Dec 22 '12 at 10:51
1

In GAE you can use the blobstore to read, write file data and to upload and download files. And you can use the 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)
voscausa
  • 11,253
  • 2
  • 39
  • 67
  • Thanks for the tips. I'm a casual programmer with only a superficial understanding so these pieces of advice are valuable to me regardless of whether I use them to solve the current issue. – Alex Bausk Dec 22 '12 at 10:54