Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python newbie with a problem writing files

Reply
Thread Tools

Re: Python newbie with a problem writing files

 
 
limodou
Guest
Posts: n/a
 
      09-04-2006
On 9/4/06, John Jones <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      09-04-2006
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

 
Reply With Quote
 
 
 
 
limodou
Guest
Posts: n/a
 
      09-05-2006
On 4 Sep 2006 08:16:24 -0700, Jason <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Files not writing, closing files, finalize() Matt Java 35 11-21-2008 07:49 AM
RE: Python code-writing for the blind. Was (Re: newbie: stani'spythoneditor if-else) Bruno Desthuilliers Python 1 09-11-2007 05:28 PM
Python newbie with a problem writing files John Jones Python 4 09-04-2006 10:39 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Help on a strategy for writing files of aprox. 0.5mb to disk at arate of 30 files per second Ben Jeurissen C++ 5 02-03-2004 04:07 AM



Advertisments