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

Session 12, Ch13-File Handling

This document discusses file handling operations in Python such as opening, reading, writing, and closing files. It explains that a file is a collection of records containing related data items. The main operations covered are creating and opening files, reading and writing text and numeric data to files, and closing files. It provides examples of opening files in different modes, writing strings and numbers to files, and reading data from files using various functions.

Uploaded by

Rajat Singh
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)
36 views13 pages

Session 12, Ch13-File Handling

This document discusses file handling operations in Python such as opening, reading, writing, and closing files. It explains that a file is a collection of records containing related data items. The main operations covered are creating and opening files, reading and writing text and numeric data to files, and closing files. It provides examples of opening files in different modes, writing strings and numbers to files, and reading data from files using various functions.

Uploaded by

Rajat Singh
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/ 13

File Handling

Prof. Rajiv Kumar


IIM Kashipur
Introduction
A file is a collection of records. A record is a group of related data
items. These data items may contain information related to
students, employees, customers, etc. In other words, a file is a
collection of numbers, symbols and text and can be considered a
stream of characters.
Various operations carried out on a file
Creating a file
Opening a file
Reading from a file
Writing to a file
Closing a file
Opening a file
Fileobject=open(File_Name,[Access_Mode], [Buffering])

Mode Description
r Opens a file for reading
w Opens a new file for writing. I a file already exists, its contents are
destroyed
a Opens a file for appending data from the end of the file
wb Opens a file for writing binary data
rb Opens a file for reading binary data
Opening a file
File object=open(File_Name, [Access_Mode], [Buffering])

Example
F1=open("Demo.txt", "r") #Open File from Current Directory
F1=open(“c:\Demo.txt", "r") #Open File located in C:

Mode Description
r Opens a file for reading
w Opens a new file for writing. I a file already exists, its contents are
destroyed
a Opens a file for appending data from the end of the file
wb Opens a file for writing binary data
rb Opens a file for reading binary data
Writing Text to a File
File object=open(File_Name, [Access_Mode], [Buffering])

Function Meaning
write (str s) Writes strings to file
Writing Text to a File
Ex. Write a program to write the sentences given below into the file Demo1.txt

Hello, How are You?


Welcome to the chapter File Handling.
Enjoy the session.

obj1=open("Demo1.txt", "w")
obj1.write("Hello, How are You? \n")
obj1.write("Welcome to the chapter File Handling. \n")
obj1.write("Enjoy the session. \n")
Closing a File
When we have finished or writing from a file, we
need to properly close it. Since an open file
consumes system resources (depending on the mode
of the file), closing it will free resources tied
to it. This is done using close() method.
Fileobject.close()

fp1=open("Demo1.txt", 'r')
fp1.close()
Writing Numbers to a File
The write() method expects a string as an argument. Therefore,
if we want to write other data types, such as integers or
floating point numbers then the numbers must be first
converted into strings before writing them to an output file.
In order to read the numbers correctly, we need to separate
them using special characters, such as “ “ (space) or ‘\n’
(new line).

Ex. Write numbers from 1 to 20 to the output file


WriteNumbers.txt

obj1 = open("WriteNumbers.txt", "w")


for x in range (1, 21):
y=str(x)
obj1.write(y)
obj1.write(" ")
obj1.close()
Reading Text from a File

Function Meaning
str read( ) Returns all the data from a file and return one complete string
str readline( ) Returns the next line of file as a string
list readlines( ) Returns a list containing all the lines in a file
str read([int number]) Returns a specified number of characters from a file. If the argument
is omitted, then the entire content of the file is read.
Reading Text from a File
fp=open("ReadDemo1.txt", "r")

text=fp.read()
print(text)

Output:
I
Love
Python
Programming
Language
Reading Text from a File
Alternative way to read a file using loop

fp=open("ReadDemo1.txt", "r")

for x in fp:
print(x)
File Pointer: tell( ) and seek( )

tell(): This is used to know the current file


pointer position

seek(offset, whence): Python file method seek()


sets the file's current position at the offset.
The whence argument is optional and defaults to 0,
which means absolute file positioning, other
values are 1 which means seek relative to the
current position and 2 means seek relative to the
file's end.

You might also like