0

I am getting the following error from the code that follows, below:

AttributeError: '_io.TextIOWrapper' object has no attribute 'write_text'

Code:

import pathlib
output_filepath = pathlib.Path(r'/home/john/somedir/data/somefilename.csv')
with output_filepath.open(mode='w') as output_file:
    for line in result_list:
        # Write records to the file
        output_file.write_text('%s\n' % line[1])

"result_list" comes from a result_list = cursor.fetchall()

The weird thing is this code is cut-and-pasted from a program that does not produce this error. Nothing is touching the object "output_filepath" in between when it gets instantiated and when it gets used in the "with" block.

I have searched The Google for the error and get zero hits (which was very surprising to me). I also looked over the various hits here (stackoverflow) that come up when you enter your "subject" for a new question.

I originally had "from pathlib import Path" as my import line, but changed it (along with the "output_filepath = ..." line) to what you see here, in my quest to find the problem.

I'm sure I'm doing something wrong somewhere, but I don't see what it is, and I don't understand why the code would work in the other program but not this one.

JohnCroc
  • 79
  • 1
  • 9

1 Answers1

0

The two objects output_filepath and output_file in your codes have different classes/types, and because of that they have different methods you can use.

You tried to use write_text, which is a method of the pathlib.Path object. However, when you're calling output_filepath.open(mode='w') you are getting an open file object in return. You can see it - Python says its type is _io.TextIOWrapper in the error message. Hence output_file.write_text doesn't work.

This open file object does not have a write_text method, but does have the write method that most files or file-like objects have in Python.

So this works:

import pathlib
output_filepath = pathlib.Path(r'/home/john/somedir/data/somefilename.csv')
with output_filepath.open(mode='w') as output_file:
    for line in result_list:
        # Write records to the file
        output_file.write('%s\n' % line[1])
unddoch
  • 5,790
  • 1
  • 24
  • 37
  • You're right, output_file.write() works, but I am unclear under what circumstances I could/should use .write_text(). I guess it's really a moot point since I now have code that works, but I would absolutely LOVE to better understand the use of .write() versus .write_text(). I've read everything I can find about .write_text() (I just started using pathlib last week), but I don't think I yet understand it very well. – JohnCroc Apr 28 '20 at 22:24
  • Sorry for not being short on explaining! I've updated the answer. Does it make sense now? – unddoch Apr 28 '20 at 22:39
  • Thank you! That's VERY helpful on the _io.TextIOWrapper side of the question and it is what I had assumed was going on. The part I don't understand, is why isn't it calling the .open() method in pathlib.Path. The object output_filepath is being instantiated as a pathlib.Path object. It seems as though that object would behave as it was instantiated and respond accordingly to the .open() call. Again, I can't tell you how grateful I am for you taking the time you have to help me understand what's going on. – JohnCroc Apr 29 '20 at 15:29