Mark Redd

16 - File Reading and Writing

When we talked about variables and memory I mentioned that there were three basic parts to the computer. They are:

This lesson is an opportunity to interact with the third part, that is the hard drive. We will learn in this lesson how to read and write files from the storage drive.

#io_files.py

ext       = '.txt'
user_name = input("Enter your name       : ")
user_age  = input("Enter your age        : ")
user_job  = input("Enter your occupation : ")
filename  = input("Enter a valid filename: ")

# let's print out what we got from the user
print(f"\nHello {user_name}! You are {user_age} years old.")
print(f"You work as a/an {user_job}")
print(f"\nSaving Your Information to \"{filename + ext}\"...", end="")

data  = user_name + "," # concatenate a string of data
data += user_age  + ','  
data += user_job  + "\n" 

# an example of the older/worse way of working with files
f = open(filename + ext , "w")   # open in write mode "w"
f.write("name,age,occupation\n") # write column headers to file
f.write(data)                    # then write data to the file
f.close()                        # save and close the file
    
print("Done.")

# better way of working with files
# always use 4 spaces to indent
with open(filename + ext, 'r') as f: # 'r' is for read
    data = f.read()                 
    print("\nHere are your data:\n---")
    print(data)
    print("---")

Here is what should happen

$ python io_files.py
Enter your name       : Mark
Enter your age        : 92
Enter your occupation : Professional Old Guy
Enter a valid filename: mark_info

Hello Mark! You are 92 years old.
You work as a/an Professional Old Guy

Saving Your Information to "mark_info.txt"...Done.

Here are your data:
---
name,age,occupation
Mark,92,Professional Old Guy

---
$

If you open the file "mark_info.txt" that should have appeared in the same folder you will see the following:

name,age,occupation
Mark,92,Professional Old Guy

What is happening here?

We are writing and reading files here with a special tool called a file object. A file object from Python’s point of view is another data type although that is not the most convenient way of understanding it. Without going too much into detail here is what is happening:

Some of these topics and nomenclature are new. I will now explain these below.

Objects

In Python, every variable you work with is an object. Objects are instances of types just as you are an instance of a human. Read that last sentence again.

A type, as I explained before, is a marker that a computer puts on a ‘box’ or set of ‘boxes’ in the RAM. It is a label that lets the computer know how to work with those boxes. An object is the actual box or set of boxes themselves.

For example, when you have a string variable such as "Tim {}", you have a set of ‘boxes’ in a certain order and each box has a bunch of 1s and 0s that represent each character in “Tim {}”. In addition, you have other sets of boxes that contain code that makes the string object’s methods work (e.g. "Tim {}".format("The Enchanter")) and any other data the string needs to function properly. Collectively, all these boxes are grouped together in memory and constitute the string object. This nomenclature will be used often so it’s important to internalize this concept early.

Other objects we have talked about such as int and float are also objects but are significantly simpler than strings. These generally have very simple data and methods associated with them but they still have the same fundamental structure as the string objects.

File Objects and Methods

Now that we have discussed objects lets look specifically at file objects. Lets begin with the open built-in function. The basic usage for the open function has the following form and arguments:

where filename is the path to the file you want to open and mode is the way you want to open the file. The basic ways you can open a file are:

These are not the only options for mode and there are ways to combine these that you can experiment with in “Hone Your Skills” below.

with Statements

To save the changes we made to the file we opened in f = open(filename + ext , "w"), we need to call f.close() on our file object. This ends up being a problem if you forget to call f.close() on the object. A better way to deal with files is with a with statement.

To be clear, the following code snippet:

f = open(filename + ext , "w")  
f.write("name,age,occupation\n")
data  = user_name + ","         
data += user_age  + ','  
data += user_job  + "\n" 
f.write(data)                   
f.close()                       

is equivalent to:

with open(filename + ext , "w") as f:
    f.write("name,age,occupation\n")
    data  = user_name + ","  
    data += user_age  + ','  
    data += user_job  + "\n" 
    f.write(data)     

Not only does the second code snippet use fewer lines of code but it is clear by looking at the indented block when you are working with the file and when you are done. The with statement simply remembers to close the file when the indented block ends. Whenever you work with files you should always use a with statement (unless you have a very good reason not to do so). Also note that working with files is not the only situation in which it is appropriate to use a with statement but it is a very common one.

A Quick Note About Function Arguments

You may have noticed the following feature of the print function on Line 12 that we have not used before:

print(f"\nSaving Your Information to \"{filename + ext}\"...", end="")

You may have rightly wondered what that business about end was. end is an optional argument in the print function. Normally when you use the print function, what is actually being printed is the string or arguments you give to the print function and a newline character (\n) is automatically appended to the end of the printed text. end is an argument that you must call by name, as seen above but with it you may override the default '\n' with anything else you wish. In the exercise we are just using an empty string.

Hone Your Skills

Remember to use with statements in all the following exercises.

Advanced Mastery