- Python Read Text File
- TL;DR
- Steps for reading a text file in Python
- 1) open() function
- 2) Reading text methods
- 3) close() method
- Reading a text file examples
- A more concise way to read a text file line by line
- Read UTF-8 text files
- Summary
- Работа с файлами в Python
- Как читать файлы
- Как читать файл по частям
- Как читать бинарные (двоичные) файлы
- Пишем в файлах в Python
- Использование оператора «with»
Python Read Text File
Summary: in this tutorial, you learn various ways to read text files in Python.
TL;DR
The 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 Python
To read a text file in Python, you follow these steps:
- First, open a text file for reading by using the open() function.
- Second, read text from the text file using the file read() , readline() , or readlines() method of the file object.
- Third, close the file using the file close() method.
1) open() function
The 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:
Mode | Description |
---|---|
‘r’ | Open for text file for reading text |
‘w’ | Open a text file for writing text |
‘a’ | Open a text file for appending text |
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 methods
The file object provides you with three methods for reading text from a text file:
- read(size) – read some contents of a file based on the optional size and return the contents as a string. If you omit the size, the read() method reads from where it left off till the end of the file. If the end of a file has been reached, the read() method returns an empty string.
- readline() – read a single line from a text file and return the line as a string. If the end of a file has been reached, the readline() returns an empty string.
- readlines() – read all the lines of the text file into a list of strings. This method is useful if you have a small file and you want to manipulate the whole text of that file.
3) close() method
The 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:
- First, when you open a file in your script, the file system usually locks it down so no other programs or scripts can use it until you close it.
- Second, your file system has a limited number of file descriptors that you can create before it runs out of them. Although this number might be high, it’s possible to open a lot of files and deplete your file system resources.
- Third, leaving many files open may lead to race conditions which occur when multiple processes attempt to modify one file at the same time and can cause all kinds of unexpected behaviors.
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 examples
We’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)
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)
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)
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 line
The 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 files
The 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)
Summary
- Use the open() function with the ‘r’ mode to open a text file for reading.
- Use the read() , readline() , or readlines() method to read a text file.
- Always close a file after completing reading it using the close() method or the with statement.
- Use the encoding=’utf-8′ to read the UTF-8 text file.
Работа с файлами в Python
В данном материале мы рассмотрим, как читать и вписывать данные в файлы на вашем жестком диске. В течение всего обучения, вы поймете, что выполнять данные задачи в Python – это очень просто. Начнем же.
Как читать файлы
Python содержит в себе функцию, под названием «open», которую можно использовать для открытия файлов для чтения. Создайте текстовый файл под названием test.txt и впишите:
Вот несколько примеров того, как использовать функцию «открыть» для чтения:
В первом примере мы открываем файл под названием test.txt в режиме «только чтение». Это стандартный режим функции открытия файлов. Обратите внимание на то, что мы не пропускаем весь путь к файлу, который мы собираемся открыть в первом примере. Python автоматически просмотрит папку, в которой запущен скрипт для text.txt. Если его не удается найти, вы получите уведомление об ошибке IOError. Во втором примере показан полный путь к файлу, но обратите внимание на то, что он начинается с «r». Это значит, что мы указываем Python, чтобы строка обрабатывалась как исходная. Давайте посмотрим на разницу между исходной строкой и обычной:
Как видно из примера, когда мы не определяем строку как исходную, мы получаем неправильный путь. Почему это происходит? Существуют определенные специальные символы, которые должны быть отображены, такие как “n” или “t”. В нашем случае присутствует “t” (иными словами, вкладка), так что строка послушно добавляет вкладку в наш путь и портит её для нас. Второй аргумент во втором примере это буква “r”. Данное значение указывает на то, что мы хотим открыть файл в режиме «только чтение». Иными словами, происходит то же самое, что и в первом примере, но более явно. Теперь давайте, наконец, прочтем файл!
Введите нижеизложенные строки в скрипт, и сохраните его там же, где и файл test.txt.
После запуска, файл откроется и будет прочитан как строка в переменную data. После этого мы печатаем данные и закрываем дескриптор файла. Следует всегда закрывать дескриптор файла, так как неизвестно когда и какая именно программа захочет получить к нему доступ. Закрытие файла также поможет сохранить память и избежать появления странных багов в программе. Вы можете указать Python читать строку только раз, чтобы прочитать все строки в списке Python, или прочесть файл по частям. Последняя опция очень полезная, если вы работаете с большими фалами и вам не нужно читать все его содержимое, на что может потребоваться вся память компьютера.
Давайте обратим внимание на различные способы чтения файлов.
Если вы используете данный пример, будет прочтена и распечатана только первая строка текстового файла. Это не очень полезно, так что воспользуемся методом readlines() в дескрипторе:
Есть вопросы по Python?
На нашем форуме вы можете задать любой вопрос и получить ответ от всего нашего сообщества!
Telegram Чат & Канал
Вступите в наш дружный чат по Python и начните общение с единомышленниками! Станьте частью большого сообщества!
Одно из самых больших сообществ по Python в социальной сети ВК. Видео уроки и книги для вас!
После запуска данного кода, вы увидите напечатанный на экране список, так как это именно то, что метод readlines() и выполняет. Далее мы научимся читать файлы по мелким частям.
Как читать файл по частям
Самый простой способ для выполнения этой задачи – использовать цикл. Сначала мы научимся читать файл строку за строкой, после этого мы будем читать по килобайту за раз. В нашем первом примере мы применим цикл:
Таким образом мы открываем файл в дескрипторе в режиме «только чтение», после чего используем цикл для его повторения. Стоит обратить внимание на то, что цикл можно применять к любым объектам Python (строки, списки, запятые, ключи в словаре, и другие). Весьма просто, не так ли? Попробуем прочесть файл по частям:
В данном примере мы использовали Python в цикле, пока читали файл по килобайту за раз. Как известно, килобайт содержит в себе 1024 байта или символов. Теперь давайте представим, что мы хотим прочесть двоичный файл, такой как PDF.
Как читать бинарные (двоичные) файлы
Это очень просто. Все что вам нужно, это изменить способ доступа к файлу:
Мы изменили способ доступа к файлу на rb, что значит read-binaryy. Стоит отметить то, что вам может понадобиться читать бинарные файлы, когда вы качаете PDF файлы из интернете, или обмениваетесь ими между компьютерами.
Пишем в файлах в Python
Как вы могли догадаться, следуя логике написанного выше, режимы написания файлов в Python это “w” и “wb” для write-mode и write-binary-mode соответственно. Теперь давайте взглянем на простой пример того, как они применяются.
ВНИМАНИЕ: использование режимов “w” или “wb” в уже существующем файле изменит его без предупреждения. Вы можете посмотреть, существует ли файл, открыв его при помощи модуля ОС Python.
Вот так вот просто. Все, что мы здесь сделали – это изменили режим файла на “w” и указали метод написания в файловом дескрипторе, чтобы написать какой-либо текст в теле файла. Файловый дескриптор также имеет метод writelines (написание строк), который будет принимать список строк, который дескриптор, в свою очередь, будет записывать по порядку на диск.
Выбирайте дешевые лайки на видео в YouTube на сервисе https://doctorsmm.com/. Здесь, при заказе, Вам будет предоставлена возможность подобрать не только недорогую цену, но и выгодные персональные условия приобретения. Торопитесь, пока на сайте действуют оптовые скидки!
Использование оператора «with»
В Python имеется аккуратно встроенный инструмент, применяя который вы можете заметно упростить чтение и редактирование файлов. Оператор with создает диспетчер контекста в Пайтоне, который автоматически закрывает файл для вас, по окончанию работы в нем. Посмотрим, как это работает: