Ex15 study drills, NameError: name 'read' is not defined

Hi all,

Use only input and try the script that way. Why is one way of getting the filename better than another?

On ex15, study drill 5, I don’t understand what is being asked for. Use only input instead of what?

With number 6, I use open("ex15_sample.txt") and get this:
<_io.TextIOWrapper name='ex15_sample.txt' mode='r' encoding='cp1252'>

Which could be what we expect but I have no idea, the drill doesn’t specify.

When attempting to use read("ex15_sample.txt"), I get:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'read' is not defined

Any help would be appreciated.

Also now that I look at it, the same is true when trying study drill 7 with close()

>>> close("ex15_sample.txt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'close' is not defined

Yes, that’s a file object: an instance of the class that provides the API for all the I/O operations that deal with files, including read and close. Bind it to a variable, then you can use those operations on it:

file = open("ex15_sample.txt")
text = file.read()
file.close()

You may want to take a look at the I/O documentation.

I think I replied to you offline but I’ll answer here too:

Try skipping this then coming back. I think you’ll get it in a few more exercises.