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

File Handling Module 1

The document provides an overview of data file handling in Python, explaining how to read from and write to disk files, which allows for permanent data storage. It details the concepts of file streams, types of files (text and binary), basic operations on data files, and how to open and close files using various modes. Additionally, it outlines the attributes of file objects and the importance of closing files to prevent data loss.

Uploaded by

drown2game
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)
7 views

File Handling Module 1

The document provides an overview of data file handling in Python, explaining how to read from and write to disk files, which allows for permanent data storage. It details the concepts of file streams, types of files (text and binary), basic operations on data files, and how to open and close files using various modes. Additionally, it outlines the attributes of file objects and the importance of closing files to prevent data loss.

Uploaded by

drown2game
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/ 3

DATA FILE HANDLING IN PYTHON(MODULE-1)

Introduction:

Data File Handling is a mechanism by which we can read data from disk files into Python program or write back
data from python program to disk files.
So far in our python program, the standard input is coming from Keyboard and output is going to Monitor i.e. no
where the data is stored permanently and the entered data is present as long as the program is running. File
Handling allows us to store data entered through python program permanently in disk file and later on we can
read back the data.

Concept of Data Files and File Streams:

 File is a location that stores data permanently pertaining to a specific location. File input/output means
transfer of data from secondary storage device to main memory and vice versa.
 The flow of data in a computer system is known as streams. There are two types of streams. Input Stream
and Output Stream. When information is read from a data file (in secondary storage), then the transfer of
data from file to the main memory is called InputStream. When information is written or recorded in a file
then the transfer of data from main memory to file (Secondary Storage) is called Output Stream.

Output Stream/Writing

(User) (Main Memory) (File on Disk)

Input Stream/Reading

 A File is a Stream or sequence of characters/data occupying named place on the disk. Python allows us to
manage two types of files. They are: (i) Text Files (ii) Binary Files

 Text File-
1. is saved with the extension .txt. It is stored in human readable form and can be created by any text
editor like Notepad.
2. Each line is terminated/delimited by a special character known as EOL (End Of Line) character. The
default EOL character in Python is ‘\n’ i.e. newline character.
3. Some internal translations take place when the EOL Character is read/written.
4. Stores information in ASCII/UNICODE format.
 Binary File-
1. Stores information in the same format as it is stored in the memory i.e. in the form of raw bits or
chunk of bytes.
2. There is no delimiter character and hence no internal translation takes place. So reading and writing
from and to to binary file is faster than text file.
3. It is extremely important to interpret stream of bytes to correct datatype while reading and writing.

Basic Operation on Data Files:

 Creating/Opening a File-

We should first open the file for read or write by specifying the name of file and mode in which the file is
to be opened.

 Performing Data Processing-

Once the file is opened- now we can either read or write for which file is opened using various functions
available. The basic operations are:

 Traversing a file for displaying data.


 Inserting data in the file.
 Appending data to the file.
 Updating data in the file.
 Deleting data from the file

 Closing the file-

After performing the required operations, we must close the file and release the file for other application
to use it.

Opening a File:

1. To open a file, we require a file variable or file object.


2. Opening a file- creates a link between the file and the file object. The link communicates with the operating
system to find the file by name and ensures that the file exists.
3. open() is used with the file object to open a file.
Syntax:
Fileobject= open(filename,<mode>)
open() takes two arguments-
i) The name of the file to be opened. It is a compulsory argument.
ii) The mode in which the file to be opened. It is an optional parameter and the default mode is
read mode i.e. reading. The different modes for opening a file are:
read i.e. r – to read from a file
write i.e. w- to write to the file
append i.e. a- to write at the end of the file
e.g.
f1=open(“test.txt”,”r”) - opens the file from default directory for reading.
f2=open(“D:\\file.txt”,”w”) - opens and creates a file in D:Drive for writing.
4. Like open () – the function file() also opens the file.
5. While creating a file- if the path is not mentioned – the file is created in the same directory of Python
interpreter.
6. On attempting to open a file in reading mode – if the file does not exist- then open() will fail resulting into
error.

Closing a file & Attributes of File Object:

1. Python automatically closes a file at the end of the program.


2. The close() closes the file manually i.e. breaks the link between the fileobject and the file after which no
operations can be performed through the fileobject.
e.g.
FileObject.close()
e.g. F.close()
3. Python automatically closes the file when the file object is assigned to another object.
4. For unexpected program termination- the operating system will not be able to write data to the file
until the file is closed explicitly. So closing a file is required.
5. While a file is opended-we can retrieve various details of the file using the following attributes-
 name- specifies the name of the file.
 mode- mode in which the file is opened.
 closed- returns True if the file is closed, otherwise False
 readable- returns True if the file is readable, otherwise False.

File Modes:

The file mode denotes how the file is opened and how it can be accessed. Different File Modes are:

Text File Mode Binary File Purpose Description


Mode
 Opens a file for reading.
‘r’ ‘rb’ read  Default Mode.
 File pointer is placed at the beginning of the file.
 Returns FileNotFoundError if the file does not exist.
‘r+’ ‘r+b’ Read  Opens the file for both reading and writing.
Or and  File pointer is placed at the beginning of the file.
‘rb+’ write  Raises IO Error if the file does not exist.
 If the file does not exist-it will be created.
‘w’ ‘wb’ write  If the file exists, then the existing contents will be
truncated and overwritten.
‘w+b’ Write  Opens the file for writing and reading.
‘w+’ Or and  If the file does not exist- a new file will be created.
‘wb+’ read  If the file exists-it will be overwritten.
 Can be used in write mode only.
‘a’ ‘ab’ append  File pointer is placed at the end.
 If the file does not exist, it will be created.
 If the file exists- all previous data will be retained
and new contents will be appended.
‘a+b’ Append  Can be used for appending and reading.
‘a+’ Or Or  File pointer is at the end.
‘ab+’ Read  If the file exists-previous contents will be retained.

You might also like