How do you convert a string obj to a file obj?
Peter Otten
__peter__ at web.de
Wed May 4 15:54:25 EDT 2005
Matthew Thorley wrote:
> Esben Pedersen wrote:
>> Matthew Thorley wrote:
>>
>>> 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
>
> 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:
>>> file.rfind
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object 'file' has no attribute 'rfind'
>>> str.rfind
<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
More information about the Python-list
mailing list