0% found this document useful (0 votes)
30 views16 pages

Unit 4

The document discusses file handling in Python. It describes that files are used to permanently store data and there are two main types - text and binary files. Text files can be opened by text editors while binary files require specific programs. Python has functions in the io module to open, read, write and close files. Files can be opened using the open() function or with clause and then read/written using methods like read(), readline(), readlines(), write(), writelines(). The seek() and tell() functions allow random accessing of files by changing the file position.

Uploaded by

Abhishek Kumar
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)
30 views16 pages

Unit 4

The document discusses file handling in Python. It describes that files are used to permanently store data and there are two main types - text and binary files. Text files can be opened by text editors while binary files require specific programs. Python has functions in the io module to open, read, write and close files. Files can be opened using the open() function or with clause and then read/written using methods like read(), readline(), readlines(), write(), writelines(). The seek() and tell() functions allow random accessing of files by changing the file position.

Uploaded by

Abhishek Kumar
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/ 16

Unit-4

File Handling
File
• A file is a named location on a secondary storage media where data are
permanently stored for later access.
• There are mainly two types of data files — text file and binary file.
• A text file consists of human readable characters, which can be opened by
any text editor.
• On the other hand, binary files are made up of non-human readable
characters and symbols, which require specific programs to access its
contents
• Files with extensions like .txt, .py, .csv, etc. are some examples of text files.
OPENING & CLOSING A TEXT FILE
• Python has the io module that contains different functions for handling files.
• 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)

• E.g :
File Open Modes
Closing a file
• While closing a file, the system frees the memory allocated to it.
• The syntax of close() is:
file_object.close()
• Here, file_object is the object that was returned while opening the
file.
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:
OR
• with open(“myfile.txt”,’r+’) as myObject:
content = myObject.read()
• 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
WRITING TO A TEXT 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
1. The write() method
write() method takes a string as an argument and writes it to the text file.
myobject=open("myfile.txt",'w’)
myobject.write("Hey I have started using files in Python\n")
myobject.close()
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.
• writelines() method does not return the number of characters written in the
file
• 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
• 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:
1) 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)
If no argument or a negative number is specified in read(), the entire file
content is read.
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).
• myobject=open("myfile.txt",'r’)
myobject.readline(10)
myobject.close()
The readlines() method
• The method reads all the lines and returns the lines along with newline as a
list of strings.
• myobject=open("myfile.txt", 'r’)
print(myobject.readlines())
myobject.close()
Q: Write a program that accepts a string from the user and writes it to a
text file. Thereafter, the same program reads the text file and displays it
on the screen.
SETTING OFFSETS IN A FILE
• 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.
1) The tell() method
• This function returns an integer that specifies the current position of the file
object in the file
• The syntax of using tell() is:
file_object.tell()
2) 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)
• 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
• It can have any of the following values:
• 0 - beginning of the file
• 1 - current position of the file
• 2 - end of file
Application of seek() and tell()

You might also like