473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 19 '05 #1
7 20350
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

fileObj = StringIO.StringIO()
fileObj.write( data )

J

Jul 19 '05 #2
John Abel 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

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'
Jul 19 '05 #3
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
Jul 19 '05 #4
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'
thanks
-Matthew
Jul 19 '05 #5
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
Jul 19 '05 #6
Peter Otten wrote:
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


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
Jul 19 '05 #7
Matthew Thorley wrote:
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
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mevar81 | last post by:
Hello to everybody.I have a problem.I used a TypeDescriptor to retrieve a TypeConverter to convert an Image to a string.The problem is that it converts the image to the string...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
3
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
5
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.