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

A Python Program to calculate Variance and Standard Deviation
Finding the variance and the standard deviation. Variance and Standard Deviation are related measures of dispersion that tell us more about a list of numbers,like are they...

Kingdom protista
The Kingdom Protista consists of eukaryotic protists. Members of this very diverse kingdom are typically unicelluar and less complex in structure than other eukaryotes. In a superficial sense, these organisms...

Science
How does a plane fly?
P

PATH institute

0 0
0

C++ Constructor and Destructor
Constructor and Destructor What is the need of a constructor and it can be explained with an example: // Turbo C compiler #inlcude #include #include class Stud { int Rno; char Name; public: void...
S

Important Questions | Indefinite Integrals (Trick) - Class 12th
The video explains the Indefinite Integrals in a way that would be easier for the students to understand. Also, several questions that are important from the exams perspective have been demonstrated along with the tricks to solve them.
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