AttributeError: 'str' object has no attribute 'write'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • loren41
    New Member
    • Mar 2010
    • 6

    AttributeError: 'str' object has no attribute 'write'

    OS is Ubuntu Linux 9.10/Python Version is 2.6.4/Gui Tool is Dr. Python

    My program has a large html-coded string called tarr_record. When I try to open an output file and write the record to the file, I receive the following error:

    AttributeError: 'str' object has no attribute 'write'

    [CODE=Python]
    open(output_fil e, "a")
    output_file.wri te(tarr_record + "\n")
    output_file.clo se()
    [/CODE]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Assign the open file object to an identifier. Call method write() on the identifier then close().
    Code:
    fileObj = open(output_file, "a")
    fileObj.write(tarr_record + "\n")
    fileObj.close()

    Comment

    Working...