Logo

dev-resources.site

for different kinds of informations.

File reading in python

Published at
7/23/2023
Categories
python
files
streams
programming
Author
mahendrap1512
Categories
4 categories in total
python
open
files
open
streams
open
programming
open
Author
13 person written this
mahendrap1512
open
File reading in python

Generally, file.read() method is used to read entire content of the file. But what if file is too big?
In such cases file.readline() and file.readlines() came to rescue.

with open('abc.txt', 'r', encoding='utf-8') as file:
    pass

Enter fullscreen mode Exit fullscreen mode

In the above code open() method return a stream/ file object, which didn't load anything in the memory. To read content of the file, we have 3 choices

1. read()

Read entire file and load all the content in the memory, optionally it can also take number of characters to load as first argument, but in most of the case we don't know the number of characters before hand.

2. readline()

Since file object return a stream, IOStream are actually iterable, so we can use next() method or for loop to get all the lines one by one, and do want-ever we want to do

3. readlines()

If no argument passed, it will also return entire file, but as a list, where one line represented by one element of the list, but you can also specify the number of lines you want to read at a time

To summerize

Python file object is a stream, and you have multiple option to read the file content, without loading entire file into the memory, so you can easily work with files without worrying about file size.

streams Article's
30 articles in total
Favicon
How does Optional.ifPresent() differ from Optional.orElse()?
Favicon
Beyond the Basics: Mastering Streams in Node.JS
Favicon
The streaming bridges — A Kafka, RabbitMQ, MQTT and CoAP example
Favicon
Java Streams | What is the difference between sorted() and distinct() in streams?
Favicon
How to handle large file uploads in SvelteKit using streams
Favicon
Execução preguiçosa com Lambdas
Favicon
Руководство по Java 8 Stream API
Favicon
In Java how to create a custom ArrayList that doesn't allow duplicate? #Interview Question
Favicon
Why Set Doesn't Allow Duplicates in Java
Favicon
Optional Class in Java and its methods
Favicon
A Few About: Streams NodeJs (PT-BR)
Favicon
File reading in python
Favicon
Asynchronous Streams in C#: Effortless Async Data Processing
Favicon
Working with streams in Node.js
Favicon
Migrating to Phoenix Liveview Streams
Favicon
What is Java Stream and why does it exist?
Favicon
-
Favicon
Streams em Node.js para iniciantes (guia básico)
Favicon
From a data stream to the data warehouse
Favicon
Gentle Intro to Node Streams
Favicon
The Java `.boxed()` Method
Favicon
Request and Response Stream - Observations
Favicon
Getting a near-real-time view of a DynamoDB stream with Python
Favicon
Exploring Java Streams
Favicon
Service Worker Side Rendering (SWSR)
Favicon
Listening to payments (real-time) on your Stellar wallet
Favicon
Supporting Cross Node Interactive Queries In Kafka Streams
Favicon
Summing Java Streams Api
Favicon
Handling Slow Servers in NodeJS
Favicon
Java 8 Non ce n’est pas que les streams

Featured ones: