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

File Operations

Uploaded by

devaki meena
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)
12 views

File Operations

Uploaded by

devaki meena
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/ 14

MASTERING FILE

OPERATIONS AND
FORMAT OPERATOR IN
PYTHON
Presented by,
Karpagayalini
Kavika
Pradeepa
Jeiya Shivani
INTRODUCTION
FILES:
A file is a container in a computer system for storing
information. Files are fundamental to how data is
organized, accessed, and managed on both local storage
devices and networked systems.
TYPES OF FILES
TEXT FILES:
Text files store data in plain text format, where each line of the
file corresponds to a sequence of characters, terminated by a
newline character.Examples: README.txt, notes.txt.
BINARY FILES:
Binary FilesContains data in a format that is not human-
readable; must be interpreted by specific software or
programs.Executable files (.exe,.bin) contains compiled code
that can be run by the computer.Examples: setup.exe, game.bin.
WHAT IS FILE HANDLING ?

FILE HANDLING:
Files are crucial for managing large amounts of
data that cannot fit into memory all at once.
Programs can read from and write to files
incrementally, making it possible to handle
large datasets.
Reading
Involves accessing data from a file for use in a
program.
Writing
Involves storing data to a file for future
reference or sharing.
Functions for File Operations:
open() readlines()
read() write()
readline() writelines()
READING A FILE
AND WRITING File Modes:
TO A FILE r - Read (default) a - Append
w - Write (truncates file) r+ - Read and write
OPENING A FILE FOR WRITING

WRITING MULTIPLE LINES:


APPENDING TO THE FILE
READING THE ENTIRE FILE
READING LINES INTO A LIST:
READING LINE BY LINE
FORMAT OPERATOR
The format operator is a powerful tool for string formatting in Python.
It allows for the insertion of variables into strings, enhancing readability
and flexibility.

PROGRAM:
name = 'Alice'
age = 30
print('Name: %s, Age: %d' % (name, age))

OUTPUT:
Name: Alice, Age: 30
FORMAT OPERATOR

FORMATTING NUMBERS: PADDING AND ALIGNMENT:

pi = 3.14159 print('%10s' % 'test') # Right-align


print('Pi: %.2f' % pi) print('%-10s' % 'test') # Left-align

OUTPUT: OUTPUT:
Pi: 3.14 test
test
OTHER FORMATTING METHODS
STR.FORMAT() METHOD:
name = "David"
age = 28
formatted_string = "Name: {}, Age: {}".format(name, age)

FORMATTED STRING LITERALS (F-STRINGS):


name = "David"
age = 28
formatted_string = f"Name: {name}, Age: {age}"

OUTPUT:
Name:David , Age:28
THANK YOU

You might also like