Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

File Handling in Python - Basic Concept

Manoj S.
21/07/2021 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 1

Please Enter a comment


Other Lessons for You

Identity operators
Identity operators Operation Syntax Function Identity a is b is_(a, b) Membership operators Operation Syntax Function Containment obj in seq contains(seq, obj)

Logarithm with JEE Example
JEE 2011 Question on Logarithm Let (x0,y0) be the solution of the following equations: (2x)ln2 = (3y)ln3 3lnx = 2lnyThen x0 is(A)1 6(B)1 3(C)1 2(D) 6Answer and Comments: (C). We can treat the data as...

2Phase Lock in Distributed Database: In this protocol, it is required that all the data items must be reached in a mutually independent manner, i.e. when one transaction is performing, then no other transaction...

Visualize your python code
Hi All, Many developers in the beginning of their career want to visualize how their code is working and the program flow. You can use the following link to visualize your python code. http://www.pythontutor.com/visualize.html#mode=edit ...

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

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