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

Discussion in 'Python' started by Matthew Thorley, May 4, 2005.

  1. 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
     
    Matthew Thorley, May 4, 2005
    #1
    1. Advertising

  2. Matthew Thorley

    John Abel Guest

    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
     
    John Abel, May 4, 2005
    #2
    1. Advertising

  3. 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'
     
    Matthew Thorley, May 4, 2005
    #3
  4. 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
     
    Esben Pedersen, May 4, 2005
    #4
  5. 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
     
    Matthew Thorley, May 4, 2005
    #5
  6. Matthew Thorley

    Peter Otten Guest

    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
     
    Peter Otten, May 4, 2005
    #6
  7. 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
     
    Matthew Thorley, May 4, 2005
    #7
  8. Matthew Thorley

    Peter Otten Guest

    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
     
    Peter Otten, May 4, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,750
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    474
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    447
    Mike C. Fletcher
    Feb 20, 2004
  4. alf
    Replies:
    9
    Views:
    414
  5. Guido Belligoi
    Replies:
    2
    Views:
    430
    Guido Belligoi
    Oct 19, 2006
Loading...

Share This Page