Re: Python newbie with a problem writing files

Discussion in 'Python' started by limodou, Sep 4, 2006.

  1. limodou

    limodou Guest

    On 9/4/06, John Jones <> wrote:
    > Hi All,
    >
    > I have been driven insane by this error. I have this small program, shown below, which runs great until it gets to writing the data out to the file. I am running an install of Python 2.4. Yes, feedparser is functioning fine (I put in print feed_title statements as tests - but have since removed them). I am missing something obvious.
    >
    > Thanks
    > JJ
    >
    > Code:
    >
    > import feedparser
    > from xml.sax import saxutils
    >
    > feed_number=200
    >
    > feed_list = open("feed_listing.conf","r")
    > for each_feed in feed_list:
    > data=feedparser.parse(each_feed)
    > feed_title=data.entries[0].title
    > xml_output=open("xml_data\\feed" + str(feed_number) + ".xml", "w")
    > xml_output.write = (feed_title)


    Maybe there is a extra '=', if it should be:

    xml_output.write(feed_title)

    ?

    > xml_output.close()
    > feed_number+=1
    >
    > Error Message:
    >
    > Traceback (most recent call last):
    > File "C:/My_Blogroll/JJ_Blogroll2", line 11, in ?
    > xml_output.write = (feed_title)
    > AttributeError: 'file' object attribute 'write' is read-only
    >


    --
    I like python!
    My Blog: http://www.donews.net/limodou
    UliPad Site: http://wiki.woodpecker.org.cn/moin/UliPad
    UliPad Maillist: http://groups.google.com/group/ulipad
    limodou, Sep 4, 2006
    #1
    1. Advertising

  2. limodou

    Jason Guest

    limodou wrote:
    > > Code:
    > >
    > > import feedparser
    > > from xml.sax import saxutils
    > >
    > > feed_number=200
    > >
    > > feed_list = open("feed_listing.conf","r")
    > > for each_feed in feed_list:
    > > data=feedparser.parse(each_feed)
    > > feed_title=data.entries[0].title
    > > xml_output=open("xml_data\\feed" + str(feed_number) + ".xml", "w")

    >
    > Maybe there is a extra '=', if it should be:
    >
    > xml_output.write(feed_title)
    >
    > ?
    >


    It took me a few moments to parse what limodou wrote here, but he's
    absolutely correct. Your problem is that you are trying to reassign
    the write method in your file. File objects are built-in types, and do
    not allow their methods to be calvaliery replaced.

    Here's an example on the correct way (and your way) of writing to a
    file:
    >>> f = open('spam.xml', 'w')
    >>> f.write( 'This data is written to the file' )
    >>> f.write = ("This is not a valid Python syntax")

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'file' object attribute 'write' is read-only
    >>>


    You'll notice that the AttributeError describes exactly what's wrong in
    this case. The write method on your file attribute is read-only. It
    doesn't say anything about whether your file is read-only.

    If you really, really want to change the write method into a string,
    you can subclass from the file class:

    >>> class MyFileType(file):

    .... pass
    ....
    >>> myfile = MyFileType('spam_and_eggs.xml', 'w')
    >>> myfile.write

    <built-in method write of MyFileType object at 0x00870B88>
    >>> myfile.write = "Gonna replace write method with this string!"
    >>> myfile.write

    'Gonna replace write method with this string!'
    >>>


    Of course, you can no longer easily access the original write method
    after re-assigning it like that.

    (limodou, I thought this might need a little bit of extra explanation
    for the original poster. I apologize if I seem rude here.)

    --Jason
    Jason, Sep 4, 2006
    #2
    1. Advertising

  3. limodou

    limodou Guest

    On 4 Sep 2006 08:16:24 -0700, Jason <> wrote:
    > limodou wrote:
    > > > Code:
    > > >
    > > > import feedparser
    > > > from xml.sax import saxutils
    > > >
    > > > feed_number=200
    > > >
    > > > feed_list = open("feed_listing.conf","r")
    > > > for each_feed in feed_list:
    > > > data=feedparser.parse(each_feed)
    > > > feed_title=data.entries[0].title
    > > > xml_output=open("xml_data\\feed" + str(feed_number) + ".xml", "w")

    > >
    > > Maybe there is a extra '=', if it should be:
    > >
    > > xml_output.write(feed_title)
    > >
    > > ?
    > >

    >
    > It took me a few moments to parse what limodou wrote here, but he's
    > absolutely correct. Your problem is that you are trying to reassign
    > the write method in your file. File objects are built-in types, and do
    > not allow their methods to be calvaliery replaced.
    >
    > Here's an example on the correct way (and your way) of writing to a
    > file:
    > >>> f = open('spam.xml', 'w')
    > >>> f.write( 'This data is written to the file' )
    > >>> f.write = ("This is not a valid Python syntax")

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: 'file' object attribute 'write' is read-only
    > >>>

    >
    > You'll notice that the AttributeError describes exactly what's wrong in
    > this case. The write method on your file attribute is read-only. It
    > doesn't say anything about whether your file is read-only.
    >
    > If you really, really want to change the write method into a string,
    > you can subclass from the file class:
    >
    > >>> class MyFileType(file):

    > ... pass
    > ...
    > >>> myfile = MyFileType('spam_and_eggs.xml', 'w')
    > >>> myfile.write

    > <built-in method write of MyFileType object at 0x00870B88>
    > >>> myfile.write = "Gonna replace write method with this string!"
    > >>> myfile.write

    > 'Gonna replace write method with this string!'
    > >>>

    >
    > Of course, you can no longer easily access the original write method
    > after re-assigning it like that.
    >
    > (limodou, I thought this might need a little bit of extra explanation
    > for the original poster. I apologize if I seem rude here.)
    >

    Very good, I just want to point out the bug according to the
    traceback, and don't think so much. But you explained so details.

    :)

    --
    I like python!
    My Blog: http://www.donews.net/limodou
    UliPad Site: http://wiki.woodpecker.org.cn/moin/UliPad
    UliPad Maillist: http://groups.google.com/group/ulipad
    limodou, Sep 5, 2006
    #3
    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. HNguyen
    Replies:
    4
    Views:
    2,398
    HNguyen
    Dec 21, 2004
  2. Ben Jeurissen
    Replies:
    5
    Views:
    426
  3. John Jones
    Replies:
    4
    Views:
    275
    Damjan
    Sep 4, 2006
  4. Bruno Desthuilliers
    Replies:
    1
    Views:
    356
    Hamilton, William
    Sep 11, 2007
  5. Matt
    Replies:
    35
    Views:
    1,369
    J. Davidson
    Nov 21, 2008
Loading...

Share This Page