How to read a text file in Python? (2024)

A text file is the file containing simple text. Python provides inbuilt functions to read, create and write text files. We will discuss how to read a text file in Python.

There are three ways to read a text file in Python −

  • read() − This method reads the entire file and returns a single string containing all the contents of the file .

  • readline()− This method reads a single line from the file and returns it as string.

  • readlines()− This method reads all the lines and return them as the list of strings.

Read a file in Python

Let there be a text file named “myfile.txt”. We need to open the file in read mode. The read mode is specified by “r”. The file can be opened using open(). The two parameters passed are the name of the file and the mode in which the file needs to be opened.

Example

file=open("myfile.txt","r")print("read function: ")print(file.read())print()file.seek(0) #Take the cursor back to begining of the file since the read() takes the cursor to the end of fileprint("readline function:")print(file.readline())print()file.seek(0) #Take the cursor back to beginning of fileprint("readlines function:")print(file.readlines())file.close()

Output

read function:This is an article on reading text files in Python.Python has inbuilt functions to read a text file.We can read files in three different ways.Create a text file which you will read later.readline function:This is an article on reading text files in Python.readlines function:['This is an article on reading text files in Python.\n', 'Python hasinbuilt functions to read a text file.\n', 'We can read files in threedifferent ways.\n', 'Create a text file which you will read later.']

As clear from the output −

The read function() reads and returns the whole file.

The readline() function reads and returns only one line.

The readlines() function reads and returns all the lines as list of the strings.

Updated on: 10-Jun-2021

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

How to read a text file in Python? (31)

Advertisem*nts

';

How to read a text file in Python? (2024)
Top Articles
How Long Will A Treated 4x4 Post Last In The Ground?
The Benefits of Purchasing Wholesale Products for Your Business
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6665

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.