How do you convert a string obj to a file obj?

  • Thread starter Matthew Thorley
  • Start date
M

Matthew Thorley

I'm writing a web app whereby a user uploads a tar acrhive which is then
opened and processed. My web form reads the file like this:

while 1:
data = value.file.read(1024 * 8) # Read blocks of 8KB at a time
if not data: break

which leaves me with data as a string obj. The problem that I have is
that the function that processes the archive expects a file object. So
far the only solution I have found it to write the file to disk and then
read it back.

Is there an easy way to convert data, in the example above into a file
object?

Thanks
-Matthew
 
J

John Abel

Matthew said:
I'm writing a web app whereby a user uploads a tar acrhive which is then
opened and processed. My web form reads the file like this:

while 1:
data = value.file.read(1024 * 8) # Read blocks of 8KB at a time
if not data: break

which leaves me with data as a string obj. The problem that I have is
that the function that processes the archive expects a file object. So
far the only solution I have found it to write the file to disk and then
read it back.

Is there an easy way to convert data, in the example above into a file
object?

Thanks
-Matthew
fileObj = StringIO.StringIO()
fileObj.write( data )

J
 
M

Matthew Thorley

John said:
fileObj = StringIO.StringIO()
fileObj.write( data )

J

I gave that a shot but got this error? Perhaps I misunderstood what kind
of object I needed. Any thoughts?

AttributeError: StringIO instance has no attribute 'rfind'
 
E

Esben Pedersen

Matthew said:
I'm writing a web app whereby a user uploads a tar acrhive which is then
opened and processed. My web form reads the file like this:

while 1:
data = value.file.read(1024 * 8) # Read blocks of 8KB at a time
if not data: break

which leaves me with data as a string obj. The problem that I have is
that the function that processes the archive expects a file object. So
far the only solution I have found it to write the file to disk and then
read it back.

Is there an easy way to convert data, in the example above into a file
object?

Thanks
-Matthew

value.file is a file object. Why don't you give that as an argument?

/Esben
 
M

Matthew Thorley

Esben said:
value.file is a file object. Why don't you give that as an argument?

/Esben

Ok I tried that but I still get this error. Any clue what I need to do?

AttributeError: 'file' object has no attribute 'rfind'


thanks
-Matthew
 
P

Peter Otten

Matthew said:
Ok I tried that but I still get this error. Any clue what I need to do?

AttributeError: 'file' object has no attribute 'rfind'

str has an rfind() method while file has not:
Traceback (most recent call last):
<method 'rfind' of 'str' objects>

So "the function" seems to expect a string rather than a file. Or perhaps
there's an (evil) isinstance(param, file) check somewhere that triggers
param to be treated as a filename. Absent further information, only you can
find out.

Peter
 
M

Matthew Thorley

Peter said:
Matthew Thorley wrote:




str has an rfind() method while file has not:



Traceback (most recent call last):


<method 'rfind' of 'str' objects>

So "the function" seems to expect a string rather than a file. Or perhaps
there's an (evil) isinstance(param, file) check somewhere that triggers
param to be treated as a filename. Absent further information, only you can
find out.

Peter

Ok you're right. tarfile.open expects a string; the path to the file
open. I thought that I had tried it with open file objects but I guess I
was mistaken.

Thanks all for the help
-Matthew
 
P

Peter Otten

Matthew said:
Ok you're right. tarfile.open expects a string; the path to the file
open. I thought that I had tried it with open file objects but I guess I
was mistaken.

tarfile.open()? That would have been important information in your initial
post.

It would have been even better to just type help(tarfile.open) at the
interactive prompt (remember that next time). Reading the documentation for
you:

"""
open([name[, mode [, fileobj[, bufsize]]]])

....

If fileobj is specified, it is used as an alternative to a file object
opened for name.
"""

So try passing value.file as the third parameter.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top