FAQ
Hello. I've come up with a small Python script but ran into trouble.
Here's what I'm trying to do : get a NNTP connection, get the last 10
headers AND bodies and save them to disk. It seems I made an error: I
try to write a list to disk using f.write() and Python complains about
this with following report

f.write(TheBody[i])
TypeError: read-only character buffer, list

Below is the script. Can someone please explain me what I'm doing
wrong? All help is greatly appreciated. Thanks

print "Start script..."
from nntplib import *
print "### Ok! Starting to read from Skynet..."
s=NNTP('news.skynet.be')
resp, count, first,last,name = s.group('comp.lang.python')
print 'Group', name, 'has', count, 'articles, range', first, 'to',
last
resp, subs = s.xhdr('subject', first + '-' + last)

print "showing and saving only last 10 headers + bodies..."
for id, sub in subs[-10:]:
print id, sub
#save body to disk
f=open( id,'w') #how do I add an extension 'txt' ???
TheBody=s.body(id)
f.write(sub)
for i in range(0,len(TheBody)):
print i,TheBody[i] #show on screen
f.write(TheBody[i])
f.close()
s.quit()
print "End of Script..."


best regards,

Daniel

Search Discussions

  • Alex Martelli at Dec 27, 2000 at 12:00 am
    "Daniel" <Daniel.Kinnaer at AdValvas.be> wrote in message
    news:3a4925f7.51675327 at news.skynet.be...
    [snip]
    try to write a list to disk using f.write() and Python complains about
    this with following report

    f.write(TheBody[i])
    TypeError: read-only character buffer, list

    Below is the script. Can someone please explain me what I'm doing
    wrong? All help is greatly appreciated. Thanks
    Python is saying: I need a read-only character buffer, you give me
    a list -- implying, a list has no conversion to such a buffer.

    If TheBody[i] is a list of strings, you can write it to file with
    f.writelines(TheBody[i])


    Alex

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 26, '00 at 11:20p
activeDec 27, '00 at 12:00a
posts2
users2
websitepython.org

2 users in discussion

Daniel: 1 post Alex Martelli: 1 post

People

Translate

site design / logo © 2022 Grokbase