0% found this document useful (0 votes)
2 views7 pages

UNIT IV - python

This document provides an overview of Python file operations, including the types of files (text and binary), how to open and close files, and methods for reading from and writing to text files. It explains the use of functions like open(), close(), read(), write(), and the use of the with clause for file handling. Additionally, it covers setting offsets in files using tell() and seek() methods for random access to file data.

Uploaded by

samsung2025.srgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

UNIT IV - python

This document provides an overview of Python file operations, including the types of files (text and binary), how to open and close files, and methods for reading from and writing to text files. It explains the use of functions like open(), close(), read(), write(), and the use of the with clause for file handling. Additionally, it covers setting offsets in files using tell() and seek() methods for random access to file data.

Uploaded by

samsung2025.srgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT IV – Python File Operations

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 and Closing a Text File

 Opening a file

To open a file in Python, we use the open() function. The syntax of open() is as follows:

file_object= open(file_name, access_mode)

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.

The syntax of close() is:

file_object.close()

Opening a file using with clause


In Python, we can also open a file using with clause. The syntax of with clause is :

with open (file_name, access_mode) as file_ object:

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.

with open(“myfile.txt”,”r+”) as myObject:

content = myObject.read()

Here, we don’t have to close the file explicitly using close() statement. Python will automatically close
the file.

Writing to a Text File


For writing to a file, we first need to open it in write or append mode. If we open an existing file in
write mode, the previous data will be erased, and the file object will be positioned at the beginning of
the file. On the other hand, in append mode, new data will be added at the end of the previous data
as the file object is at the end of the file. After opening the file, we can use the following methods to
write data in the file.

• write() - for writing a single string

• writelines() - for writing a sequence of strings

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.write("Hey I have started #using files in Python\n") 41

>>> 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.

Note: ‘\n’ is treated as a single character

The writelines() method

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()

Reading from a Text File


We can write a program to read the contents of a file. Before reading a file, we must make sure that
the file is opened in “r”, “r+”, “w+” or “a+” mode. There are three ways to read the contents of a file:

The read() method

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.read(10) 'Hello ever'

>>> 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())

Hello everyone Writing multiline strings This is the third line

>>> myobject.close()

The readline([n]) method

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.readline(10) 'Hello ever'

>>> myobject.close()

The readlines() method

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.

>>> myobject=open("myfile.txt", 'r')

>>> print(myobject.readlines()) ['Hello everyone\n', 'Writing multiline strings\n', 'This is the third line']

>>> myobject.close()

Setting Offsets in a File


The functions that we have learnt till now are used to access the data sequentially from a file. But if
we want to access data in a random fashion, then Python gives us seek() and tell() functions to do so.

The tell() method


This function returns an integer that specifies the current position of the file object in the file. The
position so specified is the byte position from the beginning of the file till the current position of the file
object.

The syntax of using tell() is:

file_object.tell()

The seek() method

This method is used to position the file object at a particular position in a file.

The syntax of seek() is:

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:

0 - beginning of the file

1 - current position of the file

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy