handle files in Python, but do you know how to read
the contents of a file? Being able to read files is essential when working with store data in Python. Python offers several built-in functions that make this easier. The three methods we'll explore in this video are read, readline, and readlines. Let's start with read. The read method returns the entire contents of the file as a string that will contain all the characters. You can also pass in an integer to return only the specified number of characters in the file. The second method to read files in Python is readline. Let's explore this method. The readline function returns a single line as a string. If for example, you have a file with two lines of texts that say, this is the first line and this is the second line. The read file function will return as the output only the first line of text. This is the first line. The readline function can also include an integer argument for returning a specified number of characters on a single line. Let's say you use the same testing file, but pass an integer of 10, your output will be the first 10 characters of the first line. In this case, the words, this is, and letters, th, for a total of 10 characters. The third method to read files in Python is readlines. Let me demonstrate this method. The readlines method reads the entire contents of the file and then returns it in an ordered list. This allows you to iterate over the list or pick out specific lines based on a condition. If for example, you have a file with four lines of texts and pass a length condition, the readfiles function will return the output, all the lines in your file in the correct order. Files are stored in directories and they have paths. Reading files from the same directory is straightforward. You only need the name of the file. When working with different locations however, it's important that you know the difference between absolute and relative paths. Let's start with absolute paths. Absolute paths contain leading forward/, or drive label. An absolute file path includes all the information you need to locate a file, whether you are in that files directory or not. Relative paths normally don't contain any reference to the root directory and are normally relative to the calling file. A relative file path only includes the information you need to locate a file in your current working directory. I'm now going to demonstrate how you can read files in Python. I start with a simple sample txt file. It just has some texts with a couple of lines that I'll use in demo some of the options there are for reading files. I started by using, with open and I pass in my file name, which is sample.txt. I just want to read in the contents, so I set the mode to be r and I sign it to a file variable. The first option to read a file is to print the entire contents of the file. To do this, I use the function print file.read, and I click on the "run button". Notice that the entire contents of the file is printed out as is. The second option allows me to print out only a certain section of the file. For example, let's say I only want to print out the quick brown fox jumps over the lazy dog. That's 44 characters. I can pass in a parameter to the read function which tells the function to read in the first 44 characters. To do this, I enter the number 44 and you notice that it prints out only the first line when I click "Run". The way this works is that it starts at the very beginning based on the index of 0, and 44 is the last character that needs to be printed out. In this way, I can control what sections are printed out. The third option I have is to read in a line. The function I want is.readline, and it will only take in the very first line from the file. I click on "Run" and it prints out only the first line of texts within that file. The fourth option is to use the.readlines function that will return a list of lines. I click on "Run" and you notice that the text in the file is now wrapped in square brackets. Lastly, because it's a list, I can assign it to a variable. For example, I can say data equals file.readlines. Then I can write a for loop for x in data. I print the value of x, and then when I click on "Run" you'll notice that the list items are printed out line by line. Something to note is that when you use the with open and you get as file, it returns a list by default. I can just change the for loop so that it points to the file variable. When I click "Run" the same option is returned. These are just some of the methods you can use in Python for reading in files. You should now be able to describe how to read files in Python and demonstrate how to output different formats using the read, readline, and readlines functions.