[Tutor] How to... (a) write to files, (b) use try/except

Rob Stevenson rob.rstevenson at gmail.com
Sun Feb 10 01:01:48 CET 2008


Hello,

Sorry to be dense, but I'm having trouble writing to a file...

I have a list of book numbers - ISBNs in a file, one per line... (see list
at bottom of this message)

my code below...

import amazon
amazon.setLicense(' -- my amazon id is in here when I run - ')

isbn_list = "c:\\ISBN.txt"
amazon.setLocale("uk")
fle=open("c:\\321.txt",'a')

for isbn in file(isbn_list):

    isbn = isbn.strip()
    try:
        books=amazon.searchByKeyword(isbn)
     except:
        x=1
    for book in books:
        try:
            Title = book.ProductName
            printstring = isbn + "\t" + book.ProductName + "\t" +
book.Authors.Author + "\tUsed from...\t" + book.UsedPrice +  "\tNew price\t"
+ book.OurPrice
            print printstring
            print printstring >> fle
        except:
           ignorance='notBliss'
fle.close


Now I have two problems

1) the file c:\321.txt (which does exist) never gets written to.  I also
tried using

fle.write(printstring)

and fle.writeline(printstring) but they didn't do anything either.

2)  if any of the function calls from amazon.py, such as
book.UsedPricefails then the rest of the details that may well have
been retreived for
that get lost in the jump to the except block.  I'd really like to get
whatever info I can on the printstring assignment line and ignore any
errors, perhaps substituting 'none' instead of a retrieved value.

Any help very appreciated.

Also, a point of nettiquette for this list - how do I say thanks to people -
not to the list, but in direct emails?

Thanks all,
Rob

==the ISBNs from my file ===


0006755151
059304083X
0349112150
0141030143
0099740915
0552773123
0747590087
0571224121
0552997706
0099490730
0141009128
0670852473
0044409672
0722540523
0375720979
1862074534
0571169945
08685506
0330481886
0333618815
057120273X
0099526409
0753509024
0099476649
0316857912
0007179731
014027443X
000637977X
1568360231
1844832007
1844832058
1844832023
1846270057
0810112434
0141022280
0552152293
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080210/66c48726/attachment.htm 


More information about the Tutor mailing list