Most programs in the real world don’t get all of their user input by calling the input function. Instead, we often read data from files on our computer, and write data back out to files as well!

Consider a program like Minecraft: whenever you load a Minecraft world, the game code opens a data file, your SAVE file, and reads in the contents of that file. It then populates the game world based on the data contained in the file. Whenever you save your Minecraft world and exit, the game writes all of the relevant data to the SAVE file again. In this way, the program does have input and output, but in the form of FILES rather than directly from the user.

So then, what does File IO (input/output) look like in Python?

The File Object

Whenever you want to open a file using a Python script, you need to create something called a File object, like so:

myFile = open("monster1.txt")

The code above is opening a text file, the name of which is the string above, monster1.txt. The myFile variable now has a File in it. The File object isn’t something we’re ready to talk about much yet, so suffice to say that this variable now represents the opened text file.

Reading from a File

There are a few different ways we can read from the file. One way is as follows:

myFile = open("monster1.txt")
allText = myFile.read()

The allText variable now has ALL OF THE TEXT from the text file in it! This could be a few lines of text, or an entire novel! Just depends on what’s in the text file.

Another way would be to read line by line instead:

#monster1.txt
Dragon
Harpy
Goblin
myFile = open("monster1.txt")
x = myFile.readline()
y = myFile.readLine()
print(x)
print(y)

The code above would print:

Dragon
Harpy

Finally, remember how we had an interesting syntax with lists and loops, where we could have the loop variable be the same value as the elements in our list? We can kinda do the same with file reading:

myFile = open("monster1.txt")
for textLine in myFile:
	print(textLine)

The code above sets the variable textLine to be each line of text in the file monster1.txt, as the loop iterates. The final output would just be… all the lines in the text file:

Dragon
Harpy
Goblin

Processing Data, Splitting Strings

One of the things we often need to do after reading the contents of a file is to process that data inside our program. If the data is in an odd format, like say:

Bulbasaur.45.49.49.65.65.45
Ivysaur.60.62.63.80.80.60
Venusaur.80.82.83.100.100.80

That might be a tricky thing to navigate! The data above represents some Pokemon and their base stats. So where do we start with processing this data?

First thing to notice is that each Pokemon is on its own line of the text file. So it might make sense to read in the data by line:

myFile = open("Pokemon.txt")
for textLine in myFile:
	#DO SOMETHING HERE!

OK, so we’re reading line by line. What if we wanted to separate the name and all the individual numbers above? We could use the split function on each string line, which would look like:

myFile = open("Pokemon.txt")
for textLine in myFile:
	listOfStrings = textLine.split(".")

So, for example, when we go through our first loop iteration, we will split the first line:

Bulbasaur.45.49.49.65.65.45

Into a list of strings:

Bulbasaur
45
49
49
65
65
45

We can now use each individual string in any way we wish!