[go: up one dir, main page]

0% found this document useful (0 votes)
13 views23 pages

File CC

Uploaded by

AMARTYA DASH
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)
13 views23 pages

File CC

Uploaded by

AMARTYA DASH
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/ 23

BCSE101E

Computer Programming: Python


Module 6 – Files

Dr. C. Chiranjeevi
School of Mechanical Engineering
Vellore Institute of Technology, Vellore

Contact Details: OPEN HOURS


Email: chiranjeevi.c@vit.ac.in Wednesday: 11.00 AM to 01.00 PM
Mobile No.: 7845190906 Friday: 11.00 AM to 01.00 PM
OFFICE: GDN#127D
10/22/2024 1
Introduction
Introduction to files:
 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.

10/22/2024 2
NEED OF FILE HANDLING
 Often the output screen of a laptop or monitor is not
enough to display all the data.
 This usually happens when the data is large and only a
limited amount can be displayed on the screen and
stored in the memory.
 Computer memory is volatile, so even if a user tries to
store the data in the memory, its contents would be lost
once a program is terminated.
 If the user needs the same data again, either it has to be
entered through a keyboard or regenerated
programmatically.
 Obviously, both these operations are tedious.
10/22/2024 3
NEED OF FILE HANDLING
 Therefore, to permanently store the data created by a
program, a user needs to save it in a File on a disk or
some other device.
 The data stored in a file is used to retrieve the user’s
information either in part or whole.
 Various operations carried out on a file are
(a) Creating a file
(b) Opening a file
(c) Reading from a file
(d) Writing to a file
(e) Closing a file
10/22/2024 4
Text Input and Output
 To read data from a file or to write data to a file, a user
needs to use the open function to first create a file
object.
Opening a File
 A file needs to open before we can perform read and
write operations on it.
 To open a file, a user needs to first create a file object
which is associated with a physical file.
 While opening a file, a user has to specify the name of
the file and its mode of operation.

10/22/2024 5
Text Input and Output
Opening a File
The syntax to open a file is:

 The above syntax to open a file returns the object for


file name.
 The mode operation used in the syntax above is a string
value which indicates how a file is going to be opened.
 The third parameter within the open function is an
optional parameter, which controls the buffering of a
file.

10/22/2024 6
Text Input and Output
Opening a File: - Buffering
If this parameter is set to 1, line buffering is performed
while accessing the file.
If the buffering value is set to 0 then no buffering takes
place.
If we specify the buffering value as an integer greater than
1 then the buffering action is performed with the indicated
buffer size.

10/22/2024 7
Text Input and Output
Different modes to open a file:
Mode Description
R Opens a file for reading.
W Opens a new file for writing. If 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.

10/22/2024 8
Text Input and Output
Writing Text to a File:
The open function creates a file object.
It is an instance of _io.TextIOWrapper class.
This class contains the methods for reading and writing
data.

10/22/2024 9
Text Input and Output
Closing a File
 When we have finished reading 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 the close() method.
The syntax to close a file is:

Example:

10/22/2024 10
Text Input and Output
Reading Text from a File:
 Once a file is opened using the open () function, its
content is loaded into the memory.
 The pointer points to the very first character of the file.
 To read the content of the file, we open the file in ‘r’
(read) mode.
The following code is used to open the file ReadDemo1.txt.

>>> fp1 = open(“ReadDemo1.txt”,”r”)

10/22/2024 11
Text Input and Output
Reading Text from a File:

There are several ways to read the content of a file.


The two common approaches are:
1) Use read() method to read all the data from a file and
return as one complete string.
2) Use readlines() method to read all data and return as a
list of strings.
The following program demonstrates the use of the read()
method to read the content of the file ReadDemo1.txt.

10/22/2024 12
Text Input and Output
Reading Text from a File:
The following program demonstrates the use of the read()
method to read the content of the file ReadDemo1.txt.
The content of the file is as shown below:

Output
I
Love
Python
Programming
Language
10/22/2024 13
Text Input and Output
Reading Numbers from a File:
Syntax used to open a file in read mode is:

The content of the file numbers.txt is


as shown below:

Write a program to read the


content of the file ‘numbers.txt’
Output
5
2
4
6
8
10
<class ‘str’>
10/22/2024 14
Text Input and Output
Reading Multiple Items on one Line:
 Many text files contain multiple items in a single line.
 The method split () for strings allows us to read more
than one piece of information in a line.
 The split () returns all the items in a list.
 In short, it splits a string into separate items and all the
items are separated by spaces or tabs.

10/22/2024 15
Text Input and Output
Reading Multiple Items on one Line:
The following example gives more details about the split()
method.

Output:

10/22/2024 16
Text Input and Output
Reading Multiple Items on one Line:
The following example gives more details about the split()
method.

Output:

10/22/2024 17
Text Input and Output
Appending Data
 The append ‘a’ mode of a file is used to append data to
the end of an existing file.
 The following program demonstrates the use of append
mode. The content of appendDemo.txt file

Output:

10/22/2024 18
THE seek() FUNCTION
 So far, it is learnt that data is stored and subsequently
read from a file in which it is stored.
 When a file is opened, we can imagine an imaginary
pointer positioned at the beginning of the file.
 What about reading the content of files from random
positions?
 Python provides an inbuilt function called seek() for
moving the pointer explicitly to any position in a file.

10/22/2024 19
THE seek() FUNCTION
 Thus, the seek() method is used to set the file pointer to
a specific position in a file.

 The syntax for seek() function is:

offset indicates the number of bytes to be moved from the


current position of the pointer and

whence indicates the point of reference from where the


bytes are to be moved from.

10/22/2024 20
THE seek() FUNCTION
The value of whence can be determined from table shown
below.

10/22/2024 21
THE seek() FUNCTION
Examples:

10/22/2024 22
Functions used in File Handling
Method Description
close() Closes the file
detach() Returns the separated raw stream from the buffer
Returns a number that represents the stream, from the operating
fileno()
system's perspective
flush() Flushes the internal buffer
isatty() Returns whether the file stream is interactive or not
read() Returns the file content
readable() Returns whether the file stream can be read or not
readline() Returns one line from the file
readlines() Returns a list of lines from the file
seek() Change the file position
seekable() Returns whether the file allows us to change the file position
tell() Returns the current file position
truncate() Resizes the file to a specified size
writable() Returns whether the file can be written to or not
write() Writes the specified string to the file
writelines()
10/22/2024
Writes a list of strings to the file 23

You might also like