UNIT IV - python
UNIT IV - python
Introduction to Files
We have so far created programs in Python that accept the input, manipulate it and display the
output. But that output is available only during execution of the program and input is to be entered
through the keyboard. This is because the variables used in a program have a lifetime that lasts till
the time the program is under execution. What if we want to store the data that were input as well as
the generated output permanently so that we can reuse it later? Usually, organizations would want to
permanently store information about employees, inventory, sales, etc. to avoid repetitive tasks of
entering the same data. Hence, data are stored permanently on secondary storage devices for
reusability. We store Python programs written in script mode with a .py extension. Each program is
stored on the secondary device as a file.
So, what is a file? A file is a named location on a secondary storage media where data are
permanently stored for later access.
Types of Files
Computers store every file as a collection of 0s and 1s i.e., in binary form. Therefore, every file is
basically just a series of bytes stored one after the other. There are mainly two types of data files —
1. text file
2. binary file
Text file
A text file can be understood as a sequence of characters consisting of alphabets, numbers and other
special symbols. Files with extensions like .txt, .py, .csv, etc.
Binary Files
Binary files are also stored in terms of bytes (0s and 1s), but unlike text files, these bytes do not
represent the ASCII values of characters. Rather, they represent the actual content such as image,
audio, video, compressed versions of other files, executable files, etc. These files are not human
readable.
Opening a file
To open a file in Python, we use the open() function. The syntax of open() is as follows:
This function returns a file object called file handle which is stored in the variable file_object.
Closing a file
Once we are done with the read/write operations on a file, it is a good practice to close the file.
Python provides a close() method to do so. While closing a file, the system frees the memory
allocated to it.
file_object.close()
The advantage of using with clause is that any file that is opened using this clause is closed
automatically, once the control comes outside the with clause. In case the user forgets to close the
file explicitly or if an exception occurs, the file is closed automatically. Also, it provides a simpler
syntax.
content = myObject.read()
Here, we don’t have to close the file explicitly using close() statement. Python will automatically close
the file.
write() method
takes a string as an argument and writes it to the text file. It returns the number of characters being
written on single execution of the write() method. Also, we need to add a newline character (\n) at the
end of every sentence to mark the end of line. Consider the following piece of code:
>>> myobject=open("myfile.txt",'w')
>>> myobject.close()
On execution, write() returns the number of characters written on to the file. Hence, 41, which is the
length of the string passed as an argument, is displayed.
This method is used to write multiple strings to a file. We need to pass an iterable object like lists,
tuple, etc. containing strings to the writelines() method. Unlike write(), the writelines() method does
not return the number of characters written in the file. The following code explains the use of
writelines().
>>> myobject=open("myfile.txt",'w')
>>> lines = ["Hello everyone\n", "Writing #multiline strings\n", "This is the #third line"]
>>> myobject.writelines(lines)
>>>myobject.close()
This method is used to read a specified number of bytes of data from a data file.
The syntax of read() method is:
file_object.read(n)
Consider the following set of statements to understand the usage of read() method:
>>>myobject=open("myfile.txt",'r')
>>> myobject.close()
If no argument or a negative number is specified in read(), the entire file content is read. For example,
>>> myobject=open("myfile.txt",'r')
>>> print(myobject.read())
>>> myobject.close()
This method reads one complete line from a file where each line terminates with a newline (\n)
character. It can also be used to read a specified number (n) of bytes of data from a file but maximum
up to the newline character (\n).
In the following example, the second statement reads the first ten characters of the first line of the text
file and displays them on the screen.
>>> myobject=open("myfile.txt",'r')
>>> myobject.close()
The method reads all the lines and returns the lines along with newline as a list of strings. The
following example uses readlines() to read data from the text file myfile.txt.
>>> print(myobject.readlines()) ['Hello everyone\n', 'Writing multiline strings\n', 'This is the third line']
>>> myobject.close()
file_object.tell()
This method is used to position the file object at a particular position in a file.
file_object.seek(offset [, reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved. reference_point
indicates the starting position of the file object. That is, with reference to which position, the offset has to be
counted. It can have any of the following values:
2 - end of file
By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file.