PSP Unit-V Notes
PSP Unit-V Notes
Instructors Name:
Name Email
Dr. B Ramana Reddy bramanareddy_cse@cbit.ac.in
Objective:
To learn the basics of File handling in Python and memory management operations
Outcome:
Scope:
Concept of files and basic file operations, writing /reading data to/from.csv file, and memory
management Operations.
Text reference:
(a) Text Book:
Jeeva Jose, “Taming Python by programming Revised edition”, Khanna publishing.
File object and Properties of file To able to access properties of file through
L.16 object the file object
Reading data from a file and Can know how to read data from a text file
L.17 writing data to a file and also how to write data to text a file
writing /reading data to/from.csv Can know how to read data from a .csv file
L.18 file and also how to write data to a .csv file
Why Files:
To Strore Input/output Data of the Program: Since Random Access Memory (RAM)
is volatile (which loses its data when the computer is turned off), we use files for future
use of the data by permanently storing them.
Data Persistence: Files allow you to store data and information in a non-volatile form,
meaning the data remains intact even after the program exits. This is vital for preserving
user settings, application state, and other information across program sessions.
Data Sharing: Files provide a universal means of sharing data between different
programs, systems, and platforms. They can be easily transferred over networks, making
them ideal for data exchange and interoperability.
Large Data Handling: Files are well-suited for handling large volumes of data that may
not fit into memory. You can read and write data in smaller chunks, enabling the
processing of datasets that are too large to hold entirely in RAM.
Data Backup: Files facilitate data backup and recovery. Regularly saving program state
and user data to files helps prevent data loss due to system failures, crashes, or accidental
deletion.
Types of Files: There are two types of files. Text files and binary files are two distinct types of
files used for different purposes in computing. Here are the key difference between them.
Text Files: Text files store data as human-readable text. They primarily contain characters from
a character encoding system like ASCII, UTF-8, or UTF-16. Text files are structured with
newline characters ('\n') to represent line breaks.
Binary Files: Binary files store data in a format that is not intended to be human-readable. They
can contain any combination of bytes, including non-printable characters. Binary files can store
images, audio, video, executables, and any data structure in its raw binary form.
Modes of Operation: In Python, when you work with files, you can specify different modes of
operation to indicate how you want to interact with the file. File modes are specified as a string
argument when opening a file using the open() function.
Text Mode – A file opened in text mode, treats its contents as if it contains text strings of
the str type. When you get data from a text mode file, Python first decodes the raw bytes
using either a platform-dependent encoding or, a specified one.
Text File
Meaning
Mode
Open for reading. The file pointer is positioned at the beginning of the file. If the
r
file doesn’t exist it will show FileNotFoundError.
Open for writing. If any data is already present in the file, it will overwrite the
w
data. If the file doesn’t exist it will create that file.
Open for exclusive creation with write. The specified file must not be available, if
x
the specified file is available it will show the error FileExistsError
Open for appending. The file pointer is positioned at the end of the file. It
a appends new data at the end of the file. If the file does not exist it will create a
new file for writing data.
r+ Open for reading and writing. It will not overwrite existing the data
w+ Open for writing and reading. It will overwrite existing data
a+ Open for appending and reading. It won't overwrite existing data
Binary File
Meaning
Mode
Open for reading. The file pointer is positioned at the beginning of the file. If the
r
file doesn’t exist it will show FileNotFoundError.
Open for writing. If any data is already present in the file, it will overwrite the
w
data. If the file doesn’t exist it will create that file.
Open for exclusive creation with write. The specified file must not be available, if
x
the specified file is available it will show the error FileExistsError
Open for appending. The file pointer is positioned at the end of the file. It
a appends new data at the end of the file. If the file does not exist it will create a
new file for writing data.
r+ Open for reading and writing. It will not overwrite existing the data
w+ Open for writing and reading. It will overwrite existing data
a+ Open for appending and reading. It won't overwrite existing data
Binary Mode – A file opened in Binary Mode, Python uses the data in the file without
any decoding, binary mode file reflects the raw data in the file.
File Operations: When we want to read from or write to a file, we need to open it first. When
we are done, it needs to be closed so that the resources that are tied with the file are freed. Hence,
in Python, a file operation takes place in the following order:
Opening a file
Reading or writing data (perform operation)
Closing the file
Positioning methods
Opening File: If we want to use a file or its data, first we have to open it.
open () – Open () function is used to open a file. It returns a pointer to the beginning of the file.
This is called a file handler or file object.
Syntax: - file_Object=open (‘filename’, ‘mode’)
• filename – It represents the name of a file.
• mode – It represents the purpose of opening the file. It defaults to 'r' which means open
for reading in text mode.
File Object: File objects can be used to access not only normal disk files but also any other
type of "file" that uses abstraction. The open() built-in function returns a file object
Syntax: file_object=open(“File_name”, “mode”)
Various Properties of File Object: Once we open a file and get a file object, we can get
various details related to that file by using its properties.
name: Name of opened file
mode: The mode in which the file is opened
closed: Returns a boolean value indicating that the file is closed or not
readable(): Returns a boolean value indicating whether file is readable or not
writable(): Returns a boolean value indicating whether the file is writable or not.
1. write () – This method is used to store/write characters or strings into the file represented by
the file object. It returns the number of characters written.
Syntax:- file_object.write(string)
2. write lines () – This method is used to store/write a group of strings (list, tuple, set) into the
file represented by the file object.
Closing a File:
1. close () – This method is used to close, an opened file.
Once we close the file, the file object is deleted from the memory hence file will be no longer
accessible unless we open it again. If you don’t explicitly close a file, Python’s garbage collector
will eventually destroy the object and close the open file for you, but the file may stay open for a
while so You should always close the open file.
Positioning Methods: tell ( ) - This method is used to find the current position of the file pointer
from the beginning of the file. The position starts from 0.
Syntax:- file_object.tell()
seek(position) – This method is used to move the file pointer from one position to another
position from the beginning of the file. The position starts from 0 and it must be a positive
integer.
Syntax:- file_object.seek(position)
With Statement: The with statement can be used while opening a file.
When we open a file using with statement there is no need to close the file explicitly.
Syntax:-with open (‘filename’, mode=‘r’) as file_object :
statements
Ex:-
with open(‘student.txt’) as f :
f.read()
Checking whether a File exists or not: file () – This method is used to check whether a
specified file exists or not. This method belongs to the path module which is a module of os
module.
Syntax:-
import os
os. path.file(filename)
Writing Data to CSV Files: To write data to a CSV file, follow these steps:
Import the csv Module:
import csv
Open the CSV File for Writing: Use the open() function with the write mode ('w') to open the
CSV file. If the file doesn't exist, it will be created.
with open('output.csv', 'w', newline='') as file:
The newline='' argument is important for cross-platform compatibility.
Create a CSV Writer:
Create a CSV writer object using csv. writer(), passing the opened file object.
csv_writer = csv.writer(file)
Write Data to the CSV: Use the writerow() method to write rows of data to the CSV file. Each
call to writerow() corresponds to a new row.
csv_writer.writerow(['Name', 'Age'])
csv_writer.writerow(['Alice', 25])
Close the File:Ensure that you close the file when you're done writing data.# The file is
automatically closed due to the 'with' statement
Example:
import csv
with open('output.csv', 'w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow(['Name', 'Age'])
csv_writer.writerow(['Alice', 25])
By following these steps, you can effectively read and write data to CSV files in Python. This is
especially useful for working with structured tabular data, such as spreadsheets or databases.
Automatic Garbage Collection: Python's cyclic garbage collector is responsible for finding and
removing objects that are no longer reachable, even when there are circular references. The
garbage collector runs in the background and cleans up memory as needed.
del Statement: The del statement can be used to remove references to objects. It doesn't
necessarily delete the object immediately, but it decrements the reference count. When the count
reaches zero, the object will be garbage collected.
x = 10
del x # Removes the reference to 10
While Python's automatic memory management is robust, in some cases, you may need to
manage memory manually, especially when working with large data structures or external
resources. Libraries like ctypes allow you to allocate and deallocate memory manually when
interfacing with external code or working with low-level data.
Memory Profiling: Python provides tools for memory profiling, such as the memory profiler
module, which allows you to monitor memory usage in your code.
Profiling helps you identify memory leaks or areas where memory usage can be optimized.
Memory Cleanup:
In some cases, memory may not be immediately released to the operating system, even after
objects are garbage collected. This is due to Python's internal memory management. However, it
is usually not a cause for concern.
Python's memory management system is designed to be convenient and automatic for most use
cases. Developers often don't need to worry about low-level memory details. However, for
specific performance-critical or memory-intensive applications, understanding memory
management principles and using memory profiling tools can be beneficial.