Expperiment 8 PPS
Expperiment 8 PPS
While copying a) all full stops are to be replaced with commas b) Lower case are to be replaced with
upper case c) upper case are to be replaced with lower case.
Objective:
Understand basic concepts of programming language and try to solve mathematical calculations in
programming approach with the help of formulas and equations.
Problem Statement:
3. Students will be able to demonstrate different Operations on any given file by using own logic.
Theory:
Python has several functions for creating, reading, updating, and deleting files.
What is a file?
File is a named location on disk to store related information. It is used to permanently store data in a
non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when computer is turned off,
we use files for future use of the data.
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 resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform operation)
3. Close the file
File Handling
The key function for working with files in Python is the open ()
function. The open () function takes two parameters; filename,
and mode.
Syntax
To open a file for reading it is enough to specify the name of the file:
f = open ("myfile.txt")
The code above is the same as:
Because "r" for read, and "t" for text are the default values, you do not need to specify
them.
How to open a file?
Python has a built-in function open() to open a file. This function returns a file object, also
called a handle, as it is used to read or modify the file accordingly.
>>> f = open("test.txt") # open file in current directory
We can specify the mode while opening a file. In mode, we specify whether we want to
read 'r',
write 'w' or append 'a' to the file. We also specify if we want to open the file in text
mode or binary mode.
The default is reading in text mode. In this mode, we get strings when reading from the file.
On the other hand, binary mode returns bytes and this is the mode to be used when
dealing with non-text files like image or exe files.
How to close a file Using Python?
When we are done with operations to the file, we need to properly close the file.
Closing a file will free up the resources that were tied with the file and is done using Python
close() method.
Python has a garbage collector to clean up unreferenced objects but, we must not rely on
it to close the file.
rite 'w' or append 'a' to the file. We also specify if we want to open the file in text
mode or binary mode.
The default is reading in text mode. In this mode, we get strings when reading from the file.
On the other hand, binary mode returns bytes and this is the mode to be used when
dealing with non-text files like image or exe files.
To read a file in Python, we must open the file in reading mode. There are various
methods available for this purpose. We can use the read(size) method to read in
size number of data. If size parameter is not specified, it reads and returns up to the
end of the file.
>>> f = open("my.txt",'r')
>>> f.read(4) # read the first 4 data
'This'
>>> f.read(4) # read the next 4 data
' is '
>>> f.read() # read in the rest till end of file
'my first file\nThis file\ncontains three lines\n'
>>> f.read() # further reading returns empty sting
''
We can see that, the read() method returns newline as '\n'. Once the end of file is reached, we get
empty string on further reading.
We can change our current file cursor (position) using the seek() method. Similarly, the tell()
method returns our current position (in number of bytes).
>>> f.tell() # get the current file position
56
>>> f.seek(0) # bring file cursor to initial position
0
>>> print(f.read()) # read the entire file
This is my first file
This file
contains three lines
Alternately, we can use readline() method to read individual lines of a file. This method reads a
file till the newline, including the newline character.
>>> f.readline()
'This is my first file\n'
>>> f.readline()
'This file\n'
>>> f.readline()
'contains three lines\n'
>>> f.readline()
''
Lastly, the readlines() method returns a list of remaining lines of the entire file. All these reading
method return empty values when end of file (EOF) is reached.
>>> f.readlines()
Conclusion:
Thus, in this experiment we have studied file handling in python successfully.