0% found this document useful (0 votes)
31 views48 pages

Unit 4

The document discusses the importance of file handling in programming, highlighting data storage, handling large datasets, data sharing, and integration with real-world applications. It covers basic syntax for file operations in Python, including reading, writing, and appending files, as well as the differences between various file modes and methods. Additionally, it explains file pointers, seeking, and how to delete files safely in Python.

Uploaded by

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

Unit 4

The document discusses the importance of file handling in programming, highlighting data storage, handling large datasets, data sharing, and integration with real-world applications. It covers basic syntax for file operations in Python, including reading, writing, and appending files, as well as the differences between various file modes and methods. Additionally, it explains file pointers, seeking, and how to delete files safely in Python.

Uploaded by

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

Unit-4

File Handling
To Upload file on Colab
Why file handling is important
1. Data Storage and Retrieval
•Files provide a way to persist data even after a program has terminated.
•Data stored in files can be read, modified, or analyzed later.

2. Handling Large Amounts of Data


•Files are useful for storing large datasets that might not fit into memory.
•By processing data in chunks, files make it easier to manage memory efficiently.

3. Data Sharing
•Files are a universal medium for sharing data between programs or systems, often in formats
like text, CSV, JSON, or XML.

4. Flexibility
•File handling allows programs to dynamically create, read, update, and delete files during
execution.
•Supports various file types (e.g., text files, binary files).
Why file handling is important

5. Integration with Real-World Applications


•Many real-world applications rely on files for logging, configurations, or
maintaining user data.
•Examples: Log files in servers, configuration files for applications, user data
storage.

6. Automation
•Automating repetitive tasks such as processing reports, generating
summaries, or analyzing logs often involves file operations.

7. Standardized APIs
•Python provides a simple and intuitive API for file handling (e.g., open(), with
statement), making it easy to work with files.
Basic Syntax
1a. read- read all the content of the file
Working of open()
• We use open () function in Python to open a file in read or write mode. As
explained above, open ( ) will return a file object.
• The syntax being:
file_variable= open(filename, mode).
• If not passed, then Python will assume it to be “ r ” by default
•"t" - Text - Default value. Text mode
•"b" - Binary - Binary mode (e.g. images)
•The default is reading in text mode. In this mode, we get strings
when reading from the file.
• file = open(‘Class1.txt', ‘r')

• File named Class1.txt will be read. Content are read into memory as
buffer and referenced the file with the reference object.

• Here, variable ‘file’ will reference a file object that we can use to read
data of the file. It will read the contents of a file as a String
Types of File Modes

•r - Read (default mode).


•w - Write (overwrites file if it exists).
•a - Append.
•rb/wb/ab - Binary mode versions.
•r+ - Read and write.
•w+, a+ - Variations for write and append with read.
•x - mode stands for exclusive creation. It is used to create
a new file, but it will raise a FileExistsError if the file
already exists
Specifying the location of file
• Suppose a current program is located in the following folder on a
C:\Users\Blake\Documents\Python
• If you specify a different path in a string literal (particularly on a
Windows computer), be sure to prefix the string with the letter r.
• E.g.:
test_file = open(r'C:\Users\Blake\temp\test.txt', 'w')
Basic Syntax
1b. readline- read first line of the file
Basic Syntax
1c. readlines- read all lines of the file
Difference in read and readlines()
Feature read() readlines()

Returns A single string A list of strings (lines)

Use Case Process entire file at once Process file line by line

Retained in each line (except the


Newline Included in the string
last line if missing)

Memory Usage Reads the entire file into memory Reads all lines but as a list
When to use what?
• Read- Small files.
• Readline- Sequential line processing.
• Readlines- Processing lines as a collection.
Basic Syntax
2a. write
Basic Syntax
Basic Syntax
2b. writelines
Basic Syntax
es
Basic Syntax
3. append
Basic Syntax
The file object atrributes:
• One we open a file. An file object is created and we can do number of
things on it.

• Here is a list of all attributes related to file object:

• file.closed: True if file is closed,otherwise return false


• file.mode: Returns access mode with which file was opened
• file.name: Return name of the file.
Another way to open a file

with open(filename,mode) as file:


Read
Readline
Readlines
Readlines
Readlines
write
writeline
writeline
Read binary
write binary
Difference in open() and with open()
Feature open with open

Resource Management Manual (file.close()) Automatic (using context manager)

Ensures file is closed even if an error


Error Handling May leave file open
occurs

Code Simplicity Requires more lines Cleaner, fewer lines

Recommended? Only for simple scripts Best practice in most cases


Seek Programming
•What is a file pointer?
Tracks the current position for reading or writing.

• seek()
used to change the position of the File Handle to a given specific position. File
handle is like a cursor, which defines from where the data has to be read or written
in the file.

• Syntax: f.seek(offset, from_what), where f is file pointer


Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.
• The reference point is selected by the from_what argument. It accepts
three values:

0: sets the reference point at the beginning of the file

1: sets the reference point at the current file position

2: sets the reference point at the end of the file

• By default from_what argument is set to 0.

• Tell() function tells the current position of the file object.


My file input:
Line 1
Line 2
Line 3
Reference Point
Command (from_What) Offset Result Valid/Invalid

seek(0, 0) Start of the file 0 Pointer stays at the start. Valid

Pointer moves to the


seek(0, 2) End of the file 0 end. Valid

seek(-5, 2) End of the file -5 Pointer moves 5 bytes Valid (Binary mode)
backward from the end.

seek(-5, 0) Start of the file -5 Error: Cannot move Invalid


before start.

Pointer stays at the


seek(0, 1) Current position 0 current position. Valid

Pointer moves 5 bytes Valid (Binary mode) but


seek(-5, 1) Current position -5 backward. only of we have read 5
bytes in forward direction
Key Point

• negative offsets can be used in binary mode when working with files in Python. This
is because binary mode ('rb', 'wb', etc.) allows the use of offsets relative to the current
position (whence=1) or the end of the file (whence=2). However, negative offsets are
not allowed in text mode, as text files are processed differently (e.g., handling line
endings).
•Binary Mode ('rb'):
•When a file is opened in
binary mode, the data is
read as raw bytes and
includes all the file's content
exactly as it is stored.
•Special characters like \b
(backspace), \r (carriage
return), or others may appear
in the output because they are
part of the file's raw data.

•Text Mode ('r'):


•In text mode, Python
automatically decodes the
content (e.g., interpreting
line endings like \r\n as \n).
Reading File In Reverse
If the file contains:
Hello
Execution Flow:
1.file.seek(0, 2) moves to the end (position = 5).
2.The loop starts:
•Iteration 1: position = 4, seek(4), read(1) → Reads 'o', prints
'o'.
•Iteration 2: position = 3, seek(3), read(1) → Reads 'l', prints
'l'.
•Iteration 3: position = 2, seek(2), read(1) → Reads 'l', prints
'l'.
•Iteration 4: position = 1, seek(1), read(1) → Reads 'e', prints
'e'.
•Iteration 5: position = 0, seek(0), read(1) → Reads 'H',
prints 'H'.
Write a program to read from one file and write its
content line by line to another file with line
numbers.
Delete a File
•Check if File exist:
To avoid getting an error, we might want to check if the file exists before we
try to delete it:
•Example
Check if file exists, then delete it:

import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")

You might also like