Find the best tutors and institutes for Python Training

Find Best Python Training classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

File Handling in Python - Basic Concept

Manoj S.
21 Jul 0 0

File (Flat) Handling in Python

Types of files in python:

1: Text file: Stores data in the form of characters. Customarily used to store text/string data.

2: Binary file: Stores data in the form of bytes. It can be used to store text, images, audio and video.

Open() Function:

To open a file, we use the open() function. Here we need to mention 'filename' and 'mode' in which we want to open that file.

Syntax: File Handler = open("FileName", "Mode")

Mode: w (write), r(read), a(append) etc.

A file that is opened should be closed with the help of the close() function.

To write in a file, we use the write() function.


A simple Python program to open, write, read and close a file.

# Open a file in write mode

f = open("myFile.txt","w")

# Take input from user

str = input("Enter text: ")

f.write(str) # Write the string into file

f.close() # Close the file

# Again open the file in read mode

print("Now Reading that file....")

f = open("myFile.txt","r")

str = f.read() # Read from file

# Display the content on the screen


f.close() # Close The file

Thank You!

0 Dislike
Follow 2

Please Enter a comment


Other Lessons for You

Mitochondria (1) Definition: (Gk – mito = thread ; chondrion = granule) Mitochondria are semi autonomous having hollow sac like structures present in all eukaryotes except mature RBCs of mammals...

How to Solve Numericals in Physics?
Are you scared of numerical? Do you think you don't understand them? If yes, then read ahead. See, it might be any exam, but numerical won't leave your back. So let's talk about common problems and their...

DBMS - SQL - Any/All
All - Operator SELECT empno, sal FROM emp WHERE sal > ALL (1999, 2999, 3999); Output of Above query is same as below query SELECT empno, sal FROM emp WHERE sal > 1999 AND sal > 2999...

Radhe Shyam | 02 Apr

0 0

Quick Revision Strategy
With exam season fast-approaching, it’s essential to start thinking about managing, planning, and effectively utilising your time. Revision plays a vital role in scoring good in your examination....

Akshaya J. | 17 Mar

1 0

Looking for Python Training Classes?

Find best tutors for Python Training Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for Python Training Classes?

Find best Python Training Classes in your locality on UrbanPro

Post your learning requirement

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more