UrbanPro
true

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.

Example:

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

print(str)

f.close() # Close The file

Thank You!

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Physics - Gauss Law - Class 12th
Here is an intuitive explanation of Gauss Law: Pre-requisites: Electric field, Area Vector, Dot Product of Vectors There are two concepts crucial to understanding the Gauss Law, first of which is the...
A

Aman

0 0
0

Cardiovascular System
The circulatory system consists of a network of artery, veins and pumping organ called the heart. The additional is the lymphatic system which is discussed separately. The circulatory system essentially...
A

Animesh R.

1 0
0

Difference between Python 2.x and Python 3.x
Python 2.x and Python 3.x differs in following features: Division operator print function Unicode xrange Error Handling __future__ module


For exam reading students
Dears, 1.All of you pls choose early morning from 430 - 7 than studying late night till 12 or 1230,will improve memory power during early hrs(Late night suppress memory). 2.Eating fruit juice like amla,pomegranade,aloe...
X

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