Python File Handling
File Handling in Python
Files can be accessed from a python program for reading the data from a file and writing the data into a file.
Opening a file in python
Python provides a function called open() to open a file programmatically. open() function returns a file object using which other file operations like reading, writing and closing of the file are done.
Syntax
Syntax :
file_object = open(file_name_path [, access_mode])
where,
- file_name_path is the filename or path of the file to be opened.
- access_mode is the mode in which the file has to be opened which is optional. If not specified, default value will be read.
- file_object is the object returned by open method which is used for further file operations.
Access mode:
- r: If the access mode specified is ‘r’, then specified file is opened in read mode, if the file exists. Otherwise if the file does not exist it will throw an error.
- w: If the access mode specified is ‘w’, then specified file is opened in write mode. If the file exists, then the content present in the file is truncated. Otherwise if the file does not exist, it will create a new file and open it for writing.
- a: If the access mode specified is ‘a’, then specified file is opened in append mode. If the file exists, the content present in the file are preserved. Otherwise if the file does not exist, it will create a new file and open it for appending.
CODE/PROGRAM/EXAMPLE
//Example :
fhr=open("data.txt","r")
Closing a file
The number of files that can be simultaneously opened by a program is limited. So it is very important to close all the files, once the operations are completed.
Syntax
Syntax:
file_object.close()
CODE/PROGRAM/EXAMPLE
//Example :
fhr.close()
Reading from file :
Consider a file data.txt with below data.
CODE/PROGRAM/EXAMPLE
this is first line
you are reading the second line
now you are dealing with third line
Reading single line:
Python provides readline() function to read the single line from the file at a time. When the end of the file reached it returns an empty string.
Syntax
//Syntax:
var_name= file_object.readline()
CODE/PROGRAM/EXAMPLE
//Example:
fhr=open("data.txt","r")
line1=fhr.readline()
print(line1,end="")
line2=fhr.readline()
print(line2,end="")
line3=fhr.readline()
print(line3,end="")
Output:
this is first line
you are reading the second line
now you are dealing with third line
Reading the contents of a file into a list:
Python provides readlines() function to read entire content of file into a variable where each line will present as an element of the list.
Syntax
//Syntax:
var=file_object.readlines()
CODE/PROGRAM/EXAMPLE
//Example:
fhr=open("data.txt","r")
list_var=fhr.readlines()
for line in list_var:
print(line,end="")
fhr.close()
Output:
this is first line
you are reading the second line
now you are dealing with third line
Reading the contents of a file into a string :
Python provides read(size) function to read the specified size(number) of characters from the file as a string into a variable. If no size is specified, then entire content of the file is read as a string into a variable.
Syntax
//Syntax:
var_name= file_object.read(size)
CODE/PROGRAM/EXAMPLE
//Example 1:
fhr=open("data.txt","r")
data =fhr.read(10)
print(data)
fhr.close()
Output:
this is fi
CODE/PROGRAM/EXAMPLE
//Example 2:
fhr=open("data.txt","r")
data =fhr.read()
print(data)
fhr.close()
Output:
this is first line
you are reading the second line
now you are dealing with third line
Iterating through file object read the content line by line:
We can also iterate through file object to read the content of the file line by line in a simple way. This method is fast and efficient compared to other methods of reading the file contents.
CODE/PROGRAM/EXAMPLE
//Example:
fhr=open("data.txt","r")
for line in fhr:
print(line,end="")
fhr.close()
Output:
this is first line
you are reading the second line
now you are dealing with third line