Reading Files
Many times, a programmer finds a reason to read content from a file. It could be that we want to read from a text file, such as a log file, or an XML file for some serious data retrieval. Sometimes, it is a massive task to figure out how to do it exactly. No worries, Python is smooth like always and makes reading files a piece of cake. There are primarily 2 ways in which Python likes to read. To keep things simple, we are just going to read from text files, feel free to explore XML on your own later. XML is a pretty cool markup, but as you get deeper, it is kind of a headache. Enough of my tangents, to the coding board:
Opening a File in Python
test.txt
I am a test file.
Maybe someday, he will promote me to a real file.
Man, I long to be a real file
and hang out with all my new real file friends.
Example
f = open("test.txt", "r") #opens file with name of "test.txt"
This is pretty simple to explain. We have our awesome little test.txt file filled with some random text. Now, in the example we have our code. Just like you click a file to open it, Python needs to know what file to open. So, we use the open method to tell Python what we want to open and to go ahead and open (make a connection to) it. The "r" just tells Python that we want to read (it is "w" when we want to write to a file). And, of course, we set this new connection to a variable so we can use it later. However, we have only opened a file, which is not all that exciting. Let's try reading from the file.
Python File Reading Methods
file.read(n)
– This method reads n number of characters from the file, or if n is blank it reads the entire file.file.readline(n)
– This method reads an entire line from the text file.
Example
f = open("test.txt","r") #opens file with name of "test.txt" print(f.read(1)) print(f.read())
Result:
I
am a test file.
Maybe someday, he will promote me to a real file.
Man, I long to be a real file
and hang out with all my new real file friends.
Woot woot! So, this might be a little confusing. First, we open the file like expected. Next, we use the read(1), and notice that we provided an argument of 1, which just means we want to read the next character. So, Python prints out "I" for us because that is the first character in the test.txt file. What happens next is a little funky. We tell Python to read the entire file with read() because we did not provide any arguments. But, it doesn't include that "I" that we just read!? It is because Python just picks up where it left off. So, if Python already read "I", it will start reading from the next character. The reason for this is so you can cycle through a file without have to skip so many characters each time you want to read a new character. It's complicated, I know, but think of reading like a one way process. Python is lazy, it doesn't want to go back and reread contents. Let's take this one step further with reading lines.
Example
f = open("test.txt","r") #opens file with name of "test.txt" print(f.readline()) print(f.readline())
Result:
I am a test file.
Maybe someday, he will promote me to a real file.
Ha! This time we were prepared for Python's trickery. Since we just used the readline() method twice, we knew that we would get first 2 lines because of Python's reading process. Of course, we also knew that readline() reads only a line because of it's simple syntax. Alright, one last important, complicated, and awesome thing about Python's reading abilities.
Example
f = open("test.txt","r") #opens file with name of "test.txt" myList = [] for line in f: myList.append(line) print(myList)
Result:
['I am a test file.\n', 'Maybe someday, he will promote me to a real file.\n', 'Man, I long to be a real file\n', 'and hang out with all my new real file friends.']
What!? Python just blew our minds! First, don't freak out with the \n. It is just a newline character, since those lines are on a different file, Python wants to keep that format (you can always strip them out later). We open the file like normal, and we create a list, which we have mastered by now. Then, we break the file into lines in our for loop using the in keyword. Python is magically smart enough to understand that it should break files into lines. Next, as we are looping through each line of the file we use myList.append(line) to add each line to our myList list. Finally, when we print it out, Python shows us its glory. It broke each line of the file into a string, which we can manipulate to do whatever we want.
Wait! Don't forget to close the file!
Example
f = open("test.txt","r") #opens file with name of "test.txt" print(f.read(1)) print(f.read()) f.close()
The important thing I saved until the end is that you should always close your files using the close() method. Python gets tired running around opening files and reading them, so give it a break and close the file to end the connection. It is always good practice to close files, your memory will thank you. Next, let's do some construction by writing to files.