Great article explaining the various/simple ways to read files in Python (keeping performance mind); with examples.
---
title: How to Properly Read Files in Python
author: net-informations.com
date: June 19, 2018
source: http://net-informations.com/python/file/read.htm
notoc: false
---
File handling in **Python** requires no importing of modules. The **File
object** provides basic functions and methods necessary to manipulate files by
default. Python gives you easy ways to manipulate these files. In order to read
data from a file, first of all, you need to open it in reading mode. Then, you
can call anyone of the methods that Python provides for reading from a file.
There are actually a number of ways to read a text file in Python. The read
functions contains different methods, `read()`, `readline()` and `readlines()`.
1. **read([number])** : Return specified number of characters from the file. if omitted it will read the entire contents of the file.
2. **readline()** : Return the next line of the file.
3. **readlines()** : Read all the lines as a list of strings in the file
## Read entire content of file at once
**example:**
with open("my_file.txt", "r") as my_file:
str = my_file.read()
print(str)
**output:**
This is first line
This is second line
This is third line
This is fourth line
## Reading only one line
**example:**
with open("my_file.txt", "r") as my_file:
str = my_file.readline()
print(str)
**output**
This is my first line
## Reading data using size
**example:**
with open("my_file.txt", "r") as my_file:
str = my_file.read(38) #read with file size
print(str)
**output**
This is my first line
This is second line
## Reading all lines as an array
**example:**
with open("my_file.txt", "r") as my_file:
str = my_file.readlines()
print(str)
**output**
['This is first line\n', 'This is second line\n', 'This is third line\n', 'This is fourth line']
## Read file line by line
If you want to read all the lines from a file in a more memory efficient, you
can use the loop over method.
**example:**
with open("my_file.txt", "r") as my_file:
for line in my_file:
print(line)
**output**
This is first line
This is second line
This is third line
This is fourth line
## Python tell() Method
The `tell()` method returns the current position of the file read/write pointer
within the file.
**example:**
with open("my_file.txt", "r") as my_file:
str = my_file.readline()
print(str)
# Get the current cursor position of the file.
pnt = my_file.tell()
print(pnt)
**output**
This is first line
20
## Python seek() method
The `seek()` method sets the file's current position at the offset.
**example:**
with open("my_file.txt", "r") as my_file:
my_file.seek(20)
str = my_file.readline()
print(str)
# bring file cursor to initial position
my_file.seek(0)
str = my_file.readline()
print(str)
**output:**
This is first line
This is second line
## Splitting lines from a text file in Python
The following Python program reading a text file and splitting it into single
words in python.
**example:**
with open("my_file.txt", "r") as my_file:
for line in my_file:
str = line.split()
print(str)
**output**
['This', 'is', 'first', 'line']
['This', 'is', 'second', 'line']
['This', 'is', 'third', 'line']
['This', 'is', 'fourth', 'line']