0% found this document useful (0 votes)
36 views5 pages

Ch5 File Handling

Files are used to store data permanently and applications often require large amounts of data stored in files on storage devices like hard disks; in Python, file handling involves opening, reading/writing to, and closing files, as well as common operations on text, binary, and CSV file types through built-in functions and modules. File pointers can also be manipulated to access and manage locations within files.

Uploaded by

Uma TNA
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)
36 views5 pages

Ch5 File Handling

Files are used to store data permanently and applications often require large amounts of data stored in files on storage devices like hard disks; in Python, file handling involves opening, reading/writing to, and closing files, as well as common operations on text, binary, and CSV file types through built-in functions and modules. File pointers can also be manipulated to access and manage locations within files.

Uploaded by

Uma TNA
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/ 5

File Handling

Files are used to store huge collection of data and records permanently.

Many applications require large amount of data. In such situation, we need to use
some devices such as hard disk, compact disc etc., to store the data.

Need for a data file

• It is a convenient way to deal with large quantities of data.

• To avoid input of data multiple times during program execution.

• To share data between various programs.

Types of file:

• Text files: Text files store information in ASCII or Unicode characters. In


text file, each line of text is terminated, (delimited) with a special character
known as EOL (End of Line) character.
• Binary files: Binary files are just files that contain information in the same
format in which the information is held in memory, i.e., in binary file, there
is no delimiter for a line.
• CSV files: CSV (Comma Separated Value) files are a common file format
for transferring and storing data. CSV is a widely used format that stores
tabular data (numbers and text) as plain text. It is a delimited text file that
uses a comma to separate values.

In Python, File Handling consists of following three steps:


• Open the file.
• Process file i.e perform read or write operation.
• Close the file.

File Manipulation
One of the key features of most object oriented programming languages is
their ability to manipulate files. This involves creating files, reading from them,
opening them, writing to them, and appending text (or data) to them.
Basic Operations on a Text File
• Open a text file: Files in Python can be opened with a built-in open()
function. The open() function creates a file object which would be utilized to call
other methods associated with it.

Syntax:
file_object=open(filename[ access_mode],[ buffering])

• filename: The file name argument is a string value that contains the
name of the file that you want to access.

• access_mode: The access_mode determines the mode in which the


file has to be opened i.e., read, write, append, etc. A complete list of
possible values is given below in the table. This is optional
parameter and the default file access mode is read (r)

File Opening Modes :


Ref Page No:

Buffering:
If the buffering value is set to 0, no buffering will take place. If the buffering
value is 1, line buffering will be performed while accessing a file. If you specify
the buffering value as an integer greater than 1, then buffering action will be
performed with the indicated buffer size. If negative, the buffer size is the system
default (default behaviour).

Close a text file: After opening and performing the reading, writing operations, it is
important to close the file. This is done using .close() method.

Syntax: file.close()

Reading and Manipulation of data from a text file:


Read and write methods. Ref.page no
Standard Input / Output and Error Streams

Python's sys module provides us with file-like objects that represent stdin, stdout, and
stderr. stdin , stdout , and stdderr variables contain stream objects corresponding to
standard I/O streams.

 Standard input: This is the file-handle that a user program reads to get information
from the user. We give input to the standard input (stdin).

 Standard output: The user program writes normal information to this file-handle.
The output is returned via the Standard output (stdout).

 Standard error: The user program writes error information to this file-handle.
Errors are returned via the Standard error (stderr). After importing sys Module we
can use these Standard Streams in the same way you use other files

Relative and Absolute Paths

The absolute path is the full path to some place on your computer. It describes how to
access a given file or directory starting from the root of the file system.

The relative path is the path to some file with respect to your current working directory
(PWD).

For example, for the file text1.txt which is store in C directory ->folder -> subfolder1 ->
subfolder2.
Absolute path: C:/folder/ subfolder1 / subfolder2/text1.txt

If PWD is C:/folder/ subfolder1 /, then the relative path to text1.txt would be:
subfolder2/text1.txt

Binary File

Open and Closing a binary file:

Binary files store data in the binary format (0’s and 1’s) which is understandable by the
machine. So when we open the binary file in our machine, it decodes the data and
displays in a humanreadable format.
Opening and closing of binary file is same as text file opening and closing. While
opening any binary file we have to specify ‘b’ in file opening mode. Hence the "rb" mode
opens the file in binary format for reading, while the "wb" mode opens the file in binary
format for writing.

Serialization:
Serialization is the process of transforming data or an object in memory (RAM) to a
stream of bytes called byte streams. These byte streams in a binary file can then be stored
in a disk or in a database or sent through a network.

De-serialization: De-serialization or unpickling is the inverse of pickling process where


a byte stream is converted back to Python object

Pickle module provides us with the ability to serialize and de-serialize objects, i.e., to
convert objects into bitstreams which can be stored into files and later be used to
reconstruct the original objects. Serialization and De-serialization process is also called
pickling and un-pickling respectively.

dump and load Methods:


Pickle module contains a dump() function to perform pickling and pickle module
contains load() function to perform unpickling.

Syntax of dump(): dump(data_object, file_object)

Where data_object is the object that has to be dumped to the file with the file handle
named file_ object.

Syntax of load(): store_object = load(file_object)

Here, the pickled Python object is loaded from the file having a file handle named
file_object and is stored in a new file handle called store_object

Accessing and Manipulating Location of File Pointer

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

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: .seek( offset[,mode])
CSV File Handling

Import CSV Module :


In Python, to work with CSV files, we need to import the CSV module, an inbuilt
module

Optional Python CSV reader/writer Parameters:

New Line : to ignore the EOL character.


Delimiter: Default delimiter is “,” Comma. Others can be mentioned in Delimiter
parameter.

Read from a csv file:

Reading from a CSV file is done using the reader object. The CSV file is opened as a text
file with Python’s built in open() function, which returns a file object.
Example: import CSV with open("Employee.txt") as CSV_file:

Write into a csv file:

we open file in ‘w’ writing mode using open() method which create newFile like object.
When we have a set of data that we would like to store in a CSV file use csv.writer()
function. To iterate the data over the rows(lines), use the csv.writerow() function.

e.g
import CSV
with open ('Employee_file.CSV', mode='w') as Employee_file:
Employee_writer = CSV.writer (Employee_file, delimiter = ',’)
Employee_writer.writerow(['Rahul', 'Manager', 'April']

You might also like