LPTHW - Exercise 15: Reading Files

LPTHW - Exercise 15

This is an interesting lesson. It may take a few minutes to get the mental abstraction of what is going on with the open() and read() commands.

Open and read?

When we work with and open files ourselves in something like a text document, we associate 'open' with 'read' i.e. we as humans read the file immediately as it opens, so we can do stuff with it.

It seems here that that's not how computers work. Remembering the old adage that you have to literally tell a computer everything it needs to do we shouldn't be surprised that we have to be extremely precise in our commands.

What is that file thing?

So, in this exercise we tell the computer to open the file and we associate the 'thing' that is this open file with the variable txt.

The term given to this open file 'thing' is a file object. You kind of have to just imagine it as a thing floating there somewhere (in memoery presumably) and ready to be used or whatever. That's why you see later in the script that read() function being used on the variable that is assigned to the file object.

Whaaaaat?

See Study Drill 1 for the lesson script with my comments. See if that helps.

from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input(">>> ")

txt_again = open(file_again)

print(txt_again.read())

txt.close()
txt_again.close()

Learn Python The Hard Way - Study Drills

1. Above each line, write out in English what that line does.

# Imports the argv (argument variable) module/library 
from sys import argv
# 'creates' two argument variables. It seems that if you use argv, you MUST use
# at least 1 argument variable for the name of you script in order for the 
# script to run properly, or more specifically you need to create two to allow
# the script to be the first arguement
script, filename = argv
# Sets variable 'txt' to function 'open()' which itself pulls in the filename
# arguement variable. This creates a 'file object' upon which further functions
# can be called. Basically 'txt' becomes a file object of the file provided
# in the argument variable. 
txt = open(filename)

print(f"Here's your file {filename}:")
# NEW - .read() is a function/method applied to the variable 'txt'. From above 'txt'
# is the file object of the file provided in the argument variable. .read() is 
# simply an instruction to read the contents of the file. As this is used in 
# conjunction with 'print' function, the content are written to the screen. 
print(txt.read())

# The below is the above repeated with alternative variable/file object names 
# instead of the file being provided via argument variable, it's provided 
# by the user via an input prompt after the script runs.
print("Type the filename again:")
file_again = input(">>> ")

txt_again = open(file_again)

print(txt_again.read())

txt.close()
txt_again.close()

2. If you are not sure ask someone for help or search online. Many times searching for "python THING" will find answers to what that THING does in Python. Try searching for "python open."

Tried it. At least on my system you get back a crappy one liner telling you to basically go and look at another command.

open(...)
    open(name[, mode[, buffering]]) -> file object
    
    Open a file using the file() type, returns a file object.  This is the
    preferred way to open a file.  See file.__doc__ for further information.

Not overly helpful!

3. I used the word "commands" here, but commands are also called "functions" and "methods." You will learn about functions and methods later in the book.

now we're getting somewhere Seinfeld gif

Get rid of the lines 10-15 where you use input and run the script again.

This would work better if you didn't tell us to write a comment above each line telling ourselves what it does. So now those line numbers are meaningless.

To combat this going forward I'll be writing out the script again and adding comments separately as I've done now with study question 1.

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

Definitely argv is better. You can use tab completion to speed up and increase the accuracy of writing the file name. When writing in the prompt you have to write the whole thing exactly, so you better remember the file path!

5. Start python3.6 to start the python shell, and use open from the prompt just like in this program. Notice how you can open files and run read on them from within python3.6?

Tried this, but I'm not sure how you are supposed to set the variable to be the ex15_sample.txt file I am using.

If you run through this script line by line, as soon as you get to setting the argv variables it returns ValueError: need more than 1 value to unpack, which is obvious it would.


Right, some playing around and the syntax you need for opening files is:

txt = open('/full/path/to/file/ex15/ex15_sample.txt')

Notice the quotes around the file path, you'll need those or you'll get an invalid systax warning.

Once you've got that then you can do stuff like txt.read().

Have your script also call close() on the txt and txt_again variables. It's important to close files when you are done with them.

Added these to the end of the script. Good job I didn't get rid of those lines 10-15 or calling close() on txt_again would be pointless.

Source files

As ever, source files on GitLab.

Spotted something wrong with this content? Tell me!

Get in touch on Twitter, GitLab or by email.