printing to a file problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    #1

    printing to a file problems

    Gang....

    I'm writing the results of a function to a file. Just a text file and here's what i've done

    >>> fn = open("c:/Python25/debt.txt", 'w')
    >>> fn.write(debtch eck())
    ['"$ 8 , 8 1 5 , 7 3 6 , 9 1 3 , 5 9 2 . 8 0 ">']
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in <module>
    TypeError: argument 1 must be string or read-only character buffer, not None

    If it makes a difference... when I do just print debtcheck() I get both a numerica response and then on the next line it says "none", as seen below

    >>> print debtcheck()
    ['"$ 8 , 8 1 5 , 7 4 4 , 1 2 5 , 6 4 2 . 1 5 ">']
    None

    What am I doing wrong for a the contents of debtcheck not being printing to the text file?

    Thanks
    PC
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    Gang....

    I'm writing the results of a function to a file. Just a text file and here's what i've done

    >>> fn = open("c:/Python25/debt.txt", 'w')
    >>> fn.write(debtch eck())
    ['"$ 8 , 8 1 5 , 7 3 6 , 9 1 3 , 5 9 2 . 8 0 ">']
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in <module>
    TypeError: argument 1 must be string or read-only character buffer, not None

    If it makes a difference... when I do just print debtcheck() I get both a numerica response and then on the next line it says "none", as seen below

    >>> print debtcheck()
    ['"$ 8 , 8 1 5 , 7 4 4 , 1 2 5 , 6 4 2 . 1 5 ">']
    None

    What am I doing wrong for a the contents of debtcheck not being printing to the text file?

    Thanks
    PC
    I am guessing that your function debtcheck() prints the string or list ['"$ 8 , 8 1 5 , 7 4 4 , 1 2 5 , 6 4 2 . 1 5 ">']. print always returns None. It will work if debtcheck() does this:
    Code:
    def debtcheck():
     return str(['"$ 8 , 8 1 5 , 7 4 4 , 1 2 5 , 6 4 2 . 1 5 ">'])

    Comment

    Working...