Write Python program to read the content of a text file and Display it on the screen
In Python, there are a few ways you can read a text file. In this article, I will go over the If you want to read a text file in Python, you first have to open it. This is the basic syntax for Python's If the text file and your current file are in the same directory ("folder"), then you can just reference the file name in the Here is an example of both files being in the same directory: If your text file is in a different
directory, then you will need to reference the correct path name for the text file. In this example, the In
order to access that file in the If you don't have the correct path for the file, then you will get an error message like this: It is really important to keep track of which directory you are in so you can reference the correct path name. There are different modes when you are working with files. The default mode is the read mode. The letter
You can also omit
There are other types of modes such as For a complete list of the other modes, please read through the documentation. Additional parameters for the open() function in PythonThe
To learn more about these optional parameters, please read through the documentation. What is the readable() method in Python?If you want to check if a file can be read, then you can use the This example would return
If I changed this example, to
What is the read() method in Python?The In this example, I am using the
This method can take in an optional parameter called size. Instead of reading the whole file, only a portion of it will be read. If we modify the earlier example, we can print out only the first word by adding
the number 4 as an argument for
If the size argument is omitted, or if the number is negative, then the whole file will be read. What is the close() method in Python?Once you are done reading a file, it is important that you close it. If you forget to close your file, then that can cause issues. This is an example of how to close the
How to use the with keyword to close files in PythonOne way to ensure that your file is closed is to use the Here is how
to rewrite our example using the
What is the readline() method in Python?This method is going to read one line from the file and return that. In this example, we have a text file with these two sentences:
If we use the
This method also takes in the optional size parameter. We can modify the example to add the number 7 to only read and print out
What is the readlines() method in Python?This method will read and return a list of all of the lines in the file. In this example, we are going to print out our grocery items as a list using the
How to use a for loop to read lines from a file in PythonAn alternative to these different read methods would be to use a In this example, we can print out all of the items in the
ConclusionIf you want to read a text file in Python, you first have to open it.
If the text file and your current file are in the same directory ("folder"), then you can just reference the file name in the If your text file is in a different directory, then you will need to reference the correct path name for the text file. The
If you want to check if a file can be read, then you can use the
The
Once you are done reading a file, it is important that you close it. If you forget to close your file, then that can cause issues.
One way to ensure that your file is closed is to use the
The
The
An alternative to these different read methods would be to use a
I hope you enjoyed this article and best of luck on
your Python journey. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |