Summary: in this tutorial, you learn various ways to read text files in Python. Show
TL;DRThe following shows how to read all texts from the readme.txt file into a string: with open('readme.txt') as f: lines = f.readlines() Code language: Python (python)Steps for reading a text file in PythonTo read a text file in Python, you follow these steps:
1) open() functionThe open() function has many parameters but you’ll be focusing on the first two: open(path_to_file, mode) Code language: Python (python)The path_to_file parameter specifies the path to the text file. If the program and file are in the same folder, you need to specify only the filename of the file. Otherwise, you need to include the path to the file as well as the filename. To specify the path to the file, you use the forward-slash ('/') even if you’re working on Windows. For example, if the file readme.txt is stored in the sample folder as the program, you need to specify the path to the file as c:/sample/readme.txt The mode is an optional parameter. It’s a string that specifies the mode in which you want to open the file. The following table shows available modes for opening a text file:
For example, to open a file whose name is the-zen-of-python.txt stored in the same folder as the program, you use the following code: f = open('the-zen-of-python.txt','r') Code language: Python (python)The open() function returns a file object which you will use to read text from a text file. 2) Reading text methodsThe file object provides you with three methods for reading text from a text file:
3) close() methodThe file that you open will remain open until you close it using the close() method. It’s important to close the file that is no longer in use for the following reasons:
The following shows how to call the close() method to close the file: f.close() Code language: Python (python)To close the file automatically without calling the close() method, you use the with statement like this: with open(path_to_file) as f: contents = f.readlines() Code language: Python (python)In practice, you’ll use the with statement to close the file automatically. Reading a text file examplesWe’ll use the-zen-of-python.txt file for the demonstration. The following example illustrates how to use the read() method to read all the contents of the the-zen-of-python.txt file into a string: with open('the-zen-of-python.txt') as f: contents = f.read() print(contents) Code language: Python (python)Output: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. ... Code language: Python (python)The following example uses the readlines() method to read the text file and returns the file contents as a list of strings: with open('the-zen-of-python.txt') as f: [print(line) for line in f.readlines()] Code language: Python (python)Output: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. ... Code language: Python (python)The reason you see a blank line after each line from a file is that each line in the text file has a newline character (\n). To remove the blank line, you can use the strip() method. For example: with open('the-zen-of-python.txt') as f: [print(line.strip()) for line in f.readlines()] Code language: Python (python)The following example shows how to use the readline() to read the text file line by line: with open('the-zen-of-python.txt') as f: while True: line = f.readline() if not line: break print(line.strip()) Code language: Python (python)Output: Explicit is better than implicit. Complex is better than complicated. Flat is better than nested. ... Code language: Python (python)A more concise way to read a text file line by lineThe open() function returns a file object which is an iterable object. Therefore, you can use a for loop to iterate over the lines of a text file as follows: with open('the-zen-of-python.txt') as f: for line in f: print(line.strip()) Code language: Python (python)This is a more concise way to read a text file line by line. Read UTF-8 text filesThe code in the previous examples works fine with ASCII text files. However, if you’re dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it’s likely a UTF-8 file that uses more than just the standard ASCII text characters. To open a UTF-8 text file, you need to pass the encoding='utf-8' to the open() function to instruct it to expect UTF-8 characters from the file. For the demonstration, you’ll use the following quotes.txt file that contains some quotes in Japanese. The following shows how to loop through the quotes.txt file: with open('quotes.txt', encoding='utf8') as f: for line in f: print(line.strip()) Code language: Python (python)Output: Summary
Did you find this tutorial helpful ? How do you use Outfile in Python?Open a file and write a short poem to the file
$ python >>> outfile = open("poem. txt", "w") >>> outfile. write("Roses are red. \n") >>> outfile.
What happens when you write to a file that has been opened for append operations?If an existing file is opened in append mode, what happens to the file's existing contents? It will not be erased and new data will be written at the end of the file's current contents.
|