File
Handling in Python Class 12 Notes
File Handling in Python
File handling is the process of saving data in a Python
programme in the form of outputs or inputs, The python file can be store in the
form of binary file or a text file. There are six different types of modes are
available in Python programming language.
1. r – read an existing file by opening it.
2. w – initiate a write operation by opening an existing file. If
the file already has some data in it, it will be overwritten; if it doesn’t
exist, it will be created.
3. a – Open a current file to do an add operation. Existing data
won’t be replaced by it.
4. r+ – Read and write data to and from the file. It will replace
any prior data in the file.
5. w+ – To write and read data, use w+. It will replace current
data.
6. a+ – Read and add data to and from the file. Existing data won’t
be replaced by it.
There are two types of
files:
Text Files- A file whose contents can be
viewed using a text editor is called a text file. A text
file is simply a
sequence of ASCII or Unicode characters. Python programs, contents written
in text editors are some
of the example of text files.e.g. .txt,.rtf,.csv etc.
Binary Files-A binary file stores the data in
the same way as as stored in the memory. The
.exe files,mp3 file,
image files, word documents are some of the examples of binary files.we
can’t read a binary file using a text editor.e.g.
.bmp,.cdr etc.
In Python, File Handling consists of following three steps
- Open the
file
- Read and
Write operation
- Close the
file
Opening and Closing files
in Python
The file must first be opened
before any reading, writing, or editing operations may be carried out. It must
first be opened in order to create any new files. The file needs to be closed
after we are finished using it. If we forgot to close the File, Python
automatically closes files when a programme finishes or a file object is no
longer referenced in a programme.
Example
open()
close()
Create a file using write() mode in Python
The write() mode is used to
creating or manipulating file in Python.
f = open("a.txt",'w')
print(f.name)#show the file name
print(f.mode)#show the mode/ type of file
line1="Welcome to python"
f.write(line1)
f.close()
Read a file using read() mode in Python
You can read the data from binary
or text file in Python using read() mode.
f = open("a.txt",'r')
text=f.read()
print(text)
f.close()
Write a file using append() mode in Python
You can add multiple line of data
in the file using append() mode in Python.
f = open("a.txt",'a')
f.write(“The text will added in
exicesiting file”)
f.close()
Flush() function in File
When you write onto a file using
any of the write functions, Python holds everthing to write in the file in
buffer and pushes it onto actual file on storage device a leter time. If
however, you want ot foce Python to write the contents of buffer onto storage,
you can use flush() function. Python automatically flushes the files when
closing them.
Example
f = open(“a.txt”, ‘w+’)
f.write(‘Welcome to my School’)
f.flush()
Next Page (CSV Type)