0% found this document useful (0 votes)
4 views10 pages

PSP Unit-V Notes

The document outlines a course on Problem Solving and Programming, focusing on file handling in Python and memory management operations. Students will learn to perform file operations, read/write CSV files, and understand memory management principles. The course includes various modules covering file types, modes of operation, and memory optimization techniques.

Uploaded by

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

PSP Unit-V Notes

The document outlines a course on Problem Solving and Programming, focusing on file handling in Python and memory management operations. Students will learn to perform file operations, read/write CSV files, and understand memory management principles. The course includes various modules covering file types, modes of operation, and memory optimization techniques.

Uploaded by

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

Course Code: 22CS C01

Course Title: PROBLEM SOLVING AND PROGRAMMING

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:

 Students will be able to perform fundamental file operations, such as creating,


opening, closing, and deleting files.
 Students will be capable of writing data to and reading data from CSV files,
understanding the structure and purpose of CSV files
 Students will have a grasp of memory management principles, including memory
allocation and deallocation techniques

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.

(b) Reference Books:


Reema Thareja, “Python programming using problem-solving approach”, Oxford
higher education.
Module/
Theme Learning Objectives
Lecture
Basics of File To understand how to create and open a
L.14
file
Types of files, File Modes Can know the different types of files, and
L.15
modes of operations

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

Memory Management Operations Can know allocation and deallocation of


L.19 memory for python objects
UNIT- V
File Handling and Memory Management
File: In Python, a file is a named location on a storage device (such as a hard drive, SSD, or
network drive) where data is stored in a structured or unstructured format. Files can be used to
store various types of data, including text, numbers, images, audio, and more. Python provides
several built-in functions and modules for working with files, making it easy to read, write, and
manipulate data stored in files.

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.

Syntax for Accessing File Properties:


name – This shows the name of the specified file.
Syntax:- file_object.name
mode – This shows the mode (purpose) of the file.
Syntax:- file_object.mode
closed – This is used to check whether the file has closed or not.
It shows True if the file is closed else shows False.
Syntax:- file_object.closed
Reading Data from a File:
1. read (size) – This method is used to read data/content from a file and returns it as a string in
text mode or bytes object in binary mode.
Syntax:- file_object.read(size)
Where size represents the number of bytes to be read from the beginning of the file.
When size is omitted or negative, the entire contents of the file will be read and returned. If the
end of the file has been reached, file_object. read() will return an empty string (‘’).
2. readline () – This method is used to read a single line from a file.
Syntax:- file_object.readline()
3. readlines () – This method is used to read all lines from a file. It will return a list of lines.
Syntax:- file_object.readlines()

Writing Data to a File:

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.

Syntax:- file_object.write lines(group of string)

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.

What will happen if we do not close the opened file: -

• Data of the file may be corrupted or deleted.


• Memory utilized by the file is not freed it may cause insufficient memory.

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)

Reading Data from CSV Files:


CSV (Comma-Separated Values) files are a common format for storing tabular data. Python
provides the CSV module to work with CSV files efficiently. Here's how to read data from a
CSV file:

Import the csv Module:


Import the csv module to access its functions and classes.
import csv
Open the CSV File:
Use the open() function to open the CSV file in read mode ('r').
with open('data. csv', 'r') as file:
Create a CSV Reader: Create a CSV reader object using csv. reader(), passing the opened file
object.
csv_reader = csv.reader(file)
Read Data from the CSV: You can now iterate through the CSV rows and access the data.
for row in csv_reader:
print(row)
This will print each row as a list of values.
Close the File: Ensure that you close the file when you're done.
# File is automatically closed due to the 'with' statement
Example:
import csv
with open('data. csv', 'r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
print(row)

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.

Memory management Operations:


Memory management in Python is largely automated thanks to the language's built-in memory
management system. Python uses a technique called reference counting combined with a cyclic
garbage collector to manage memory efficiently. However, there are still some memory
management considerations and operations you should be aware of:
Reference Counting: Python uses reference counting to keep track of how many references
(pointers) exist to each object in memory. When the reference count drops to zero, meaning there
are no more references to an object, Python's garbage collector deallocates the memory occupied
by that object.

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

Manual Memory Management:

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 Optimization Techniques: Avoid creating unnecessary objects or data structures.


Reuse objects whenever possible to reduce memory overhead. Use generator expressions or
iterators instead of creating large lists when processing data. Use built-in data types and libraries
that are optimized for memory efficiency, such as NumPy for numerical computations. Be
mindful of circular references, as they can prevent objects from being garbage collected. Use
weak references when necessary.

Memory Debugging Tools:


Tools like guppy, objgraph, and memory profilers can help diagnose memory issues in your
Python code.

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.

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