Unit Iii File Handling Functions
Unit Iii File Handling Functions
Unit Iii File Handling Functions
A) FILE HANDLING
Python file handling refers to the process of working with files on the filesystem.
File handling in Python is a powerful and versatile tool that can be used to
perform a wide range of operations.
The four primary functions used for file handling in Python are:
open() : Opens a file and returns a file object.
read() : Reads data from a file.
write() : Writes data to a file.
close() : Closes the file, releasing its resources.
Each line of a file is terminated with a special character, called the EOL or End of
Line characters like comma {,} or newline character.
Files can be categorized into two types based on their mode of operation:
Text Files : These store data in plain text format. Examples
include .txt files.
Binary Files : These store data in binary format, which is not human-
readable. Examples include images, videos, and executable files.
Another way
To create a new file, we have to open a file using one of the two parameters:
x: it will create a new empty text file iff there does not exist any file with the
same name; otherwise, it will throw an error
w: it will create a file, whether any file exists with the same or not, i.e., it will not
return an error if the same file name exists.
nl = open ('nl.txt', 'x') # it will create a new empty file but will throw an error if the
same file name exists
nl = open ('nl.txt', 'w') # it will create a file but if the same file name exist it will
overwrite
File-Object - returns a file object called file handle which is stored in the
variable file_object.
Filename: name of a file that has to be to open
access_mode is an optional argument that represents the mode in which the file
has to be accessed
w+: To write and read data. It overwrites the previous file if one exists, it
will truncate the file to zero length or create a file if it does not exist.
a+: To append and read data from the file. It won’t override existing data.
Read(size)
The read() method returns the specified number of bytes from the file.
It takes the size as parameter.
It is optional, means number of bytes to return.
Example
print (file.read())
Output:
Hello world
Welcome
123 456
# Python code to illustrate read() mode
print (file.read(5))
Output:
Hello
Readline([n])
Example
f = open("sample.txt", "r")
print(f.readline())
Output:
Helloworld
Readlines()
The readlines() method returns all the line from the file.
Example
f = open("sample.txt", "r")
print(f.readlines())
Output:
Helloworld
Welcome
123 456
Write()
Writelines()
write() : Inserts the string str1 in a single line in the text file.
File_object.write(str1)
writelines() : For a list of string elements, each string is inserted in the text file.
Used to insert multiple strings at a single time.
Each string in the list is written to the file sequentially without adding any
newline characters automatically.
File_object.writelines(L)
for L = [str1, str2, str3]
file = open('sample.txt','w')
file.close()
Output:
This is the write command
It allows us to write in a particular file
We can also use the written statement along with the with() function.
# Python code to illustrate with() alongwith write()
f.write("Hello World!!!")
Output:
Hello World!!!
file.close()
Output:
This is the write command
It allows us to write in a particular file
This will add this line
Closing a file
You should always close your files, in some cases, due to buffering, changes
made to a file may not show until you close the file.
Syntax
file.close()
file.close()
Rename() -
Syntax
os.rename(current_file_name, new_file_name)
Remove()
Syntax
os.remove(file_name)