0% found this document useful (0 votes)
6 views

File Handling Class 12 SN

The document provides an overview of file handling in Python, detailing types of data files (text and binary), methods for opening, closing, reading, and writing files, as well as handling CSV files. It explains functions like read(), write(), and the use of the csv module for managing tabular data. Additionally, it covers important concepts such as file modes, error handling, and the significance of file pointers.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

File Handling Class 12 SN

The document provides an overview of file handling in Python, detailing types of data files (text and binary), methods for opening, closing, reading, and writing files, as well as handling CSV files. It explains functions like read(), write(), and the use of the csv module for managing tabular data. Additionally, it covers important concepts such as file modes, error handling, and the significance of file pointers.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

File Handling Short Notes

04 October 2023 09:47

Data Files: Files that store related data about a specific application for later use. They are of 2 types.

1. Text Files: A text file stores information in the form of ASCII or Unicode character. In text files each line of text
is terminated, with a special character know as EOL (End Of Line). In python, this EOL character is the new line
character ('\n'). Text files can be of following types.

(i) Regular Text Files: These are the text files which store the text in the same form as typed. Here \n
(newline character) ends a line. These have file extensions as .txt.

(ii) Delimited Text Files: In these text files, a specific character is stored to separate the values. E.g. TSV
Files (Tab character is used to separate values), CSV Files (Comma is used to separate values)

2. Binary Files: A binary file stores the information in the form of a stream of bytes. A binary file contains
information in the same format in which the information is held in the memory, i.e. the file content is returned
to you is raw (without any translation or encoding). In binary files there are no delimiters for a line.

Opening & Closing Files

1. Opening Files: The first thing we do while handling a file is opening the file. Python provides a function called
as the open().
Syntax:
<file_objectname> = open(<filename>)
<file_objectname> = open(<filename>,<mode>)
Ex:

Here, school.txt is opened in write mode is attached to the file-object namely file.
File-object is also known as file-handle.
Note: The default file-open mode is read ('r') mode. That is if you do not provide any file open mode.

FileNotFoundError: When a file is opened in read mode, the file should already exist in the folder, else python
will throw FileNotFoundError.

File-Object: It is a reference to a file on disk. It opens and makes it available for a number of different tasks.
They are used to read and write data to a file on a disk.

2. Closing Files: An open file is closed by calling the close(). This function breaks the link of file-object and the file
on the disk. After close(), no tasks can be performed on that file thorough the file-object.
Syntax:
<fileobject>.close()
Ex:

Note: Closing files using close() is a good programming practice.

Reading from Text Files

1. read() : Reads at most 'n' number of bytes. If 'n' is not specified, it reads the entire file. Returns the read

File Handling Page 1


1. read() : Reads at most 'n' number of bytes. If 'n' is not specified, it reads the entire file. Returns the read
bytes in form of a string.
Syntax:
<filehandle>.read([n])
Ex:

Output:

2. readline() : Reads one line of input, if 'n' is specified reads at most 'n' bytes. Returns the read bytes in the
form of a string ending with a line character '\n' and returns a blank string if no more bytes are left for reading
the file. When you use len() and check the size the length will be 1 more than the actually length of the string
due to the line character \n.
Syntax:
<filehandle>.readline([n])
Ex:

Output:

3. readlines() : Reads all the lines in the file and returns what is read in the form of a list.
Syntax:
<filehandle>.readlines()
Ex:

Output:

Writing onto Text Files

1. write() : Writes string into a file referenced by <filehandle>.


Syntax:
<filehandle>.write(str1)
Ex:

Output:

File Handling Page 2


Output:

2. writelines() : Writes all the strings in a list L as lines into a file referenced by <filehandle>.
Syntax:
<filehandle>.writelines(L)
Ex:

Output:

Appending a File

When we open a file in 'w' or write mode, python overwrites an existing file. Which means a file with the same
name, the previous data which was in the file is lost. If we want to retain the data and as well as write onto the
file we open the file in 'a' or append mode. A file opened in append mode retains its previous data while
allowing us to add newer data into it. This can also be done by adding '+' with a file in read mode.

The flush() Function

When we write onto a file using any write function, python holds everything to write in the file buffer and then
pushes it onto the file storage later. However, if we want to write the content that is stored in the buffer onto
the file storage we use the flush().
It basically forces the writing of data on disc still pending in output buffer.

Removing White Spaces after Reading from a File

All the reading functions read the trailing and leading whitespaces. In order to remove these whitespaces we
can use the strip() functions.

The strip() removes the given character from both ends.


The rstrip() removes the given character from trailing end, i.e. right end.
The lstrip() removes the given character from leading end, i.e. left end.

With Statement: The with statement is very handy. It automatically closes the file after the nested block of the
code. It guarantees that the file will be closed.
Syntax:
with open(<filename>,<filemode>) as <filehandle>:
<file manipulation statements>
Ex:

File Handling Page 3


Absolute and Relative Paths

Path is a sequence of directory names which give you the authority to access a particular directory or file name.
The full name of a file or a directory is also called pathname.
The Absolute paths are from the topmost level of directory structure. The relative paths are relative to current
working directory as dot(.) while its parent directory two dots(..).

Working with Binary Files

1. Serialisation (Pickling) : It is the process of converting a python object like dictionaries, lists etc. into a byte
stream so that it can be written into a file.
2. Unpickling: It is the reverse process of serialisation where a byte stream is converted into a python object.
Unpickling produces the exact replica of the original object.

To achieve this in python we use the pickle module. To implement this in our code we first import the pickle
module.
After importing the module we use the dump() and load() of the pickle module to write and read from an open
binary file respectively.

Creating/Opening/Closing Binary Files

Syntax:
<filehandle> = open(<filename>,<filemode'b'>)
Ex:

Here, we use .dat extension indicating that it is binary file.


To close the file we use,
f.close() or <filehandle>.close

Writing onto a Binary File - Pickling

In order to write an object onto a binary file opened in write mode, we are supposed to use the dump() function
of the pickle module.
Syntax :
pickle.dump(<object-to-be-written>,<filehandle>)
Ex:

File Handling Page 4


Python allows us to pickle objects of, Boolean, Int, Float, Complex Number, Strings, Tuples, Lists, Sets,
Dictionaries etc.

Appending Records in Binary Files: To append records in a binary file we need to make sure we open the file in
append mode('ab'or 'ab+').

Reading from a Binary File - Unpickling

To read from the pickled file we need to open the file in 'rb' mode and use the load() as it unpickles the data
coming from the pickled file.
Syntax:
<object> = pickle.load(<filehandle>)
Ex:

Output:

Note: We need to understand that when the pickle.load() function reaches the end-of-file it will raise EOFError.
We can handle this error by using try and except blocks.

Using try and except blocks:

To implement this method we must write pickle.load() enclosed in try and except statements.:
Syntax:
<filehandle> = open(<filename>,<readmode>)
try:
<object> = pickle.load(<filehandle>)
except EOFError:
<filehandle>.close()
Ex:

File Handling Page 5


Ex:

Output:

Accessing and Manipulating Location of File Pointer

1. tell() Function: The tell() function returns the current position of file pointer in the file.
Syntax:
<fileobject>.tell()
Ex:

Output:

Output:

2. seek() Function: The seek() function changes the position of the file pointer by placing the file-pointer at the
specified position in the open file.
Syntax:
<fileobject>.seek(n)
Ex:

Output:

Note: While modifying a binary file we need to make sure that the datatypes do not get changed for the value
being modified. Such changes may affect pickling and unpickling process sometimes and leads to Unpickling
Error.
pickle.picklingError : Raised when an unpickleable object is encountered while writing.
pickle.UnpicklingError: Raised during unpickling of an object, if there is any problem.

Working with CSV Files

File Handling Page 6


Working with CSV Files

Csv files are delimited files that store tabular data(data stored in rows and columns) where comma delimits
every value, i.e. the values are separate with comma. Typically the default delimiter in CSV files is the comma (,)
but we can choose a different delimiter character as well. The separator character is called a delimiter.

The better way of handling CSV files is by using the csv module in python. The other methods include using the
procedure that was used on text files.

CSV Module of Python: The csv module provides functionality to read and write tabular data in CSV format. It
provides us with two specific object types - the reader and writer object - to read and write into the CSV Files.

Opening and Closing CSV Files

Here, we need to use the .csv extension.


Ex:

Output:

Note: Csv files are popular as it is easier to create, capable of storing large amounts of data etc.

newline Argument: We can also specify an additional argument 'newline' which is an optional but important
argument. The role of this argument is to specify how python would handle newline characters while working
with CSV files.
Syntax:

This argument basically ensures that no translation of EOL character takes place.

Writing in CSV Files

1. csv.writer() : Returns a writer object which writes data into CSV files.
2. <writerobject>.writerow() : Writes one row of data onto the writer object.
3. <writerobject>.writerows() : Writes multiple rows of data onto the writer object.

Writer object converts the received user data in the form which is appropriate for the csv files.

The writerow() Function:


Ex:

The writerows() Function:


Ex:

File Handling Page 7


Reading in CSV Files

For reading from a csv file we use the csv.reader() function. This function returns a reader object which loads
data from csv file into an iterable after parsing delimited data.
Ex:

Output:

The above output contains these empty rows as we didn't specify the 'newline argument' in the file open
function while creating the file. To rectify it we do the below operation which is adding the newline argument
while creating the file.
Ex:

Now Reading into the file

Output:

File Handling Page 8


File Handling Page 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy