python file handling

Python File Handling
Till now, we were taking the input from the console and writing it back to the console to interact with the user.
Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again.
Opening a file
Python provides the open() function which accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc.
The syntax to use the open() function is given below.
file object = open(<file-name>, <access-mode>, <buffering>)   
The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.
SN Access mode Description
1 r It opens the file to read-only. The file pointer exists at the beginning. The file is by default open in this mode if no access mode is passed.
2 rb It opens the file to read only in binary format. The file pointer exists at the beginning of the file.
3 r+ It opens the file to read and write both. The file pointer exists at the beginning of the file.
4 rb+ It opens the file to read and write both in binary format. The file pointer exists at the beginning of the file.
5 w It opens the file to write only. It overwrites the file if previously exists or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.
6 wb It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.
7 w+ It opens the file to write and read both. It is different from r+ in the sense that it overwrites the previous file if one exists whereas r+ doesn't overwrite the previously written file. It creates a new file if no file exists. The file pointer exists at the beginning of the file.
8 wb+ It opens the file to write and read both in binary format. The file pointer exists at the beginning of the file.
9 a It opens the file in the append mode. The file pointer exists at the end of the previously written file if exists any. It creates a new file if no file exists with the same name.
10 ab It opens the file in the append mode in binary format. The pointer exists at the end of the previously written file. It creates a new file in binary format if no file exists with the same name.
11 a+ It opens a file to append and read both. The file pointer remains at the end of the file if a file exists. It creates a new file if no file exists with the same name.
12 ab+ It opens a file to append and read both in binary format. The file pointer remains at the end of the file.
Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read mode and printing its content on the console.
Example
#opens the file file.txt in read mode 
fileptr = open("file.txt","r") 
 
if fileptr: 
    print("file is opened successfully") 
Output:
<class '_io.TextIOWrapper'>
file is opened successfully

The close() method
Once all the operations are done on the file, we must close it through our python script using the close() method. Any unwritten information gets destroyed once the close() method is called on a file object.
We can perform any operation on the file externally in the file system is the file is opened in python, hence it is good practice to close the file once all the operations are done.
The syntax to use the close() method is given below.
fileobject.close() 
Consider the following example.
Example
# opens the file file.txt in read mode 
fileptr = open("file.txt","r") 
 
if fileptr: 
    print("file is opened successfully") 
 
#closes the opened file 
fileptr.close() 
Reading the file
To read a file using the python script, the python provides us the read() method. The read() method reads a string from the file. It can read the data in the text as well as binary format.
The syntax of the read() method is given below.
fileobj.read(<count>) 
Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the count is not specified, then it may read the content of the file until the end.
Consider the following example.
Example
#open the file.txt in read mode. causes error if no such file exists. 
fileptr = open("file.txt","r"); 
 
#stores all the data of the file into the variable content 
content = fileptr.read(9); 
 
# prints the type of the data stored in the file 
print(type(content)) 
 
#prints the content of the file 
print(content) 
 
#closes the opened file 
fileptr.close() 
Output:
<class 'str'>
Hi, I am
Read Lines of the file
Python facilitates us to read the file line by line by using a function readline(). The readline() method reads the lines of the file from the beginning, i.e., if we use the readline() method two times, then we can get the first two lines of the file.
Consider the following example which contains a function readline() that reads the first line of our file "file.txt"containing three lines.
Example
#open the file.txt in read mode. causes error if no such file exists. 
fileptr = open("file.txt","r"); 
 
#stores all the data of the file into the variable content 
content = fileptr.readline(); 
 
# prints the type of the data stored in the file 
print(type(content)) 
 
#prints the content of the file 
print(content) 
 
#closes the opened file 
fileptr.close() 
Output:
<class 'str'>
Hi, I am the file and being used as
Looping through the file
By looping through the lines of the file, we can read the whole file.
Example
#open the file.txt in read mode. causes an error if no such file exists. 
 
 
fileptr = open("file.txt","r"); 
 
#running a for loop 
for i in fileptr: 
    print(i) # i contains each line of the file 
Output:
Hi, I am the file and being used as
an example to read a
file in python.
Writing the file
To write some text to a file, we need to open the file using the open method with one of the following access modes.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file exists.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
Consider the following example.
Example 1
#open the file.txt in append mode. Creates a new file if no such file exists. 
fileptr = open("file.txt","a"); 
 
#appending the content to the file 
fileptr.write("Python is the modern day language. It makes things so simple.") 
 
 
#closing the opened file 
fileptr.close(); 
Now, we can see that the content of the file is modified.
File.txt:
Hi, I am the file and being used as 
an example to read a 
file in python. 
Python is the modern day language. It makes things so simple. 
Example 2
#open the file.txt in write mode. 
fileptr = open("file.txt","w"); 
 
#overwriting the content of the file 
fileptr.write("Python is the modern day language. It makes things so simple.") 
 
 
#closing the opened file 
fileptr.close(); 
Now, we can check that all the previously written content of the file is overwritten with the new text we have passed.
File.txt:
Python is the modern day language. It makes things so simple. 
Creating a new file
The new file can be created by using one of the following access modes with the function open(). x: it creates a new file with the specified name. It causes an error a file exists with the same name.
a: It creates a new file with the specified name if no such file exists. It appends the content to the file if the file already exists with the specified name.
w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.
Consider the following example.
Example
#open the file.txt in read mode. causes error if no such file exists. 
fileptr = open("file2.txt","x"); 
 
print(fileptr) 
 
if fileptr: 
    print("File created successfully"); 
Output:
File created successfully
Using with statement with files
The with statement was introduced in python 2.5. The with statement is useful in the case of manipulating the files. The with statement is used in the scenario where a pair of statements is to be executed with a block of code in between.
The syntax to open a file using with statement is given below.
with open(<file name>, <access mode>) as <file-pointer>: 
    #statement suite 
The advantage of using with statement is that it provides the guarantee to close the file regardless of how the nested block exits.
It is always suggestible to use the with statement in the case of file s because, if the break, return, or exception occurs in the nested block of code then it automatically closes the file. It doesn't let the file to be corrupted.
Consider the following example.
Example
with open("file.txt",'r') as f: 
    content = f.read(); 
    print(content) 
Output:
Python is the modern day language. It makes things so simple.
File Pointer positions
Python provides the tell() method which is used to print the byte number at which the file pointer exists. Consider the following example.
Example
# open the file file2.txt in read mode 
fileptr = open("file2.txt","r") 
 
#initially the filepointer is at 0 
print("The filepointer is at byte :",fileptr.tell()) 
 
#reading the content of the file 
content = fileptr.read(); 
 
#after the read operation file pointer modifies. tell() returns the location of the fileptr. 
 
print("After reading, the filepointer is at:",fileptr.tell()) 
Output:
The filepointer is at byte : 0
After reading, the filepointer is at 26
Modifying file pointer position
In the real world applications, sometimes we need to change the file pointer location externally since we may need to read or write the content at various locations.
For this purpose, the python provides us the seek() method which enables us to modify the file pointer position externally.
The syntax to use the seek() method is given below.
<file-ptr>.seek(offset[, from) 
The seek() method accepts two parameters:
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the beginning of the file is used as the reference position. If it is set to 1, the current position of the file pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the reference position.
Consider the following example.
Example
# open the file file2.txt in read mode 
fileptr = open("file2.txt","r") 
 
#initially the filepointer is at 0 
print("The filepointer is at byte :",fileptr.tell()) 
 
#changing the file pointer location to 10. 
fileptr.seek(10); 
 
#tell() returns the location of the fileptr. 
print("After reading, the filepointer is at:",fileptr.tell()) 
Output:
The filepointer is at byte : 0
After reading, the filepointer is at 10
Python os module
The os module provides us the functions that are involved in file processing operations like renaming, deleting, etc.
Let's look at some of the os module functions.
Renaming the file
The os module provides us the rename() method which is used to rename the specified file to a new name. The syntax to use the rename() method is given below.
rename(?current-name?, ?new-name?) 
Example
import os; 
 
#rename file2.txt to file3.txt 
os.rename("file2.txt","file3.txt") 
Removing the file
The os module provides us the remove() method which is used to remove the specified file. The syntax to use the remove() method is given below.
remove(?file-name?) 
Example
import os; 
 
#deleting the file named file3.txt 
os.remove("file3.txt") 
Creating the new directory
The mkdir() method is used to create the directories in the current working directory. The syntax to create the new directory is given below.
mkdir(?directory name?) 
Example
import os; 
 
#creating a new directory with the name new 
os.mkdir("new") 
Changing the current working directory
The chdir() method is used to change the current working directory to a specified directory.
The syntax to use the chdir() method is given below.
chdir("new-directory") 
Example
import os; 
 
#changing the current working directory to new 
 
os.chdir("new") 
The getcwd() method
This method returns the current working directory.
The syntax to use the getcwd() method is given below.
os.getcwd() 
Example
import os; 
 
#printing the current working directory 
print(os.getcwd()) 
Deleting directory
The rmdir() method is used to delete the specified directory.
The syntax to use the rmdir() method is given below.
os.rmdir(?directory name?) 
Example
import os; 
 
#removing the new directory 
os.rmdir("new") 
The file related methods
The file object provides the following methods to manipulate the files on various operating systems.
SN Method Description
1 file.close() It closes the opened file. The file once closed, it can't be read or write any more.
2 File.fush() It flushes the internal buffer.
3 File.fileno() It returns the file descriptor used by the underlying implementation to request I/O from the OS.
4 File.isatty() It returns true if the file is connected to a TTY device, otherwise returns false.
5 File.next() It returns the next line from the file.
6 File.read([size]) It reads the file for the specified size.
7 File.readline([size]) It reads one line from the file and places the file pointer to the beginning of the new line.
8 File.readlines([sizehint]) It returns a list containing all the lines of the file. It reads the file until the EOF occurs using readline() function.
9 File.seek(offset[,from) It modifies the position of the file pointer to a specified offset with the specified reference.
10 File.tell() It returns the current position of the file pointer within the file.
11 File.truncate([size]) It truncates the file to the optional specified size.
12 File.write(str) It writes the specified string to a file
13 File.writelines(seq) It writes a sequence of the strings to a file.



Python Modules
A python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module.
Modules in Python provides us the flexibility to organize the code in a logical way.
To use the functionality of one module into another, we must have to import the specific module.
Example
In this example, we will create a module named as file.py which contains a function func that contains a code to print some message on the console.
Let's create the module named as file.py.
#displayMsg prints a message to the name being passed. 
def displayMsg(name) 
    print("Hi "+name);   
Here, we need to include this module into our main module to call the method displayMsg() defined in the module named file.
Loading the module in our python code
We need to load the module in our python code to use its functionality. Python provides two types of statements as defined below.
The import statement
The from-import statement
The import statement
The import statement is used to import all the functionality of one module into another. Here, we must notice that we can use the functionality of any python source file by importing that file as the module into another python source file.
We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times, it has been imported into our file.
The syntax to use the import statement is given below.
import module1,module2,........ module n 
Hence, if we need to call the function displayMsg() defined in the file file.py, we have to import that file as a module into our module as shown in the example below.
Example:
import file; 
name = input("Enter the name?") 
file.displayMsg(name) 
Output:
Enter the name?John
Hi John
The from-import statement
Instead of importing the whole module into the namespace, python provides the flexibility to import only the specific attributes of a module. This can be done by using from? import statement. The syntax to use the from-import statement is given below.
from < module-name> import <name 1>, <name 2>..,<name n> 
Consider the following module named as calculation which contains three functions as summation, multiplication, and divide.
calculation.py:
#place the code in the calculation.py 
def summation(a,b): 
    return a+b 
def multiplication(a,b): 
    return a*b; 
def divide(a,b): 
    return a/b; 
Main.py:
from calculation import summation   
#it will import only the summation() from calculation.py 
a = int(input("Enter the first number")) 
b = int(input("Enter the second number")) 
print("Sum = ",summation(a,b)) #we do not need to specify the module name while accessing summation() 
Output:
Enter the first number10
Enter the second number20
Sum =  30
The from...import statement is always better to use if we know the attributes to be imported from the module in advance. It doesn't let our code to be heavier. We can also import all the attributes from a module by using *.
Consider the following syntax.
from <module> import * 
Renaming a module
Python provides us the flexibility to import some module with a specific name so that we can use this name to use that module in our python source file.
The syntax to rename a module is given below.
import <module-name> as <specific-name> 
Example
#the module calculation of previous example is imported in this example as cal. 
import calculation as cal; 
a = int(input("Enter a?")); 
b = int(input("Enter b?")); 
print("Sum = ",cal.summation(a,b)) 
Output:
Enter a?10
Enter b?20
Sum =  30
Using dir() function
The dir() function returns a sorted list of names defined in the passed module. This list contains all the sub-modules, variables and functions defined in this module.
Consider the following example.
Example
import json 
 
List = dir(json) 
 
print(List) 
Output:
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__',
'_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
The reload() function
As we have already stated that, a module is loaded once regardless of the number of times it is imported into the python source file. However, if you want to reload the already imported module to re-execute the top-level code, python provides us the reload() function. The syntax to use the reload() function is given below.
reload(<module-name>) 
for example, to reload the module calculation defined in the previous example, we must use the following line of code.
reload(calculation) 
Scope of variables
In Python, variables are associated with two types of scopes. All the variables defined in a module contain the global scope unless or until it is defined within a function.
All the variables defined inside a function contain a local scope that is limited to this function itself. We can not access a local variable globally.
If two variables are defined with the same name with the two different scopes, i.e., local and global, then the priority will always be given to the local variable.
Consider the following example.
Example
name = "john" 
def print_name(name): 
    print("Hi",name) #prints the name that is local to this function only. 
name = input("Enter the name?") 
print_name(name) 
Output:
Hi David

Python packages
The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. The packages are used to categorize the application level code efficiently.
Let's create a package named Employees in your home directory. Consider the following steps.
1. Create a directory with name Employees on path /home.
2. Create a python source file with name ITEmployees.py on the path /home/Employees.
                                                                                                                                                                                                                                                   
def getITNames(): 
    List = ["John", "David", "Nick",    "Martin"] 
    return List; 
3. Similarly, create one more python file with name BPOEmployees.py and create a function getBPONames().
4. Now, the directory Employees which we have created in the first step contains two python modules. To make this directory a package, we need to include one more file here, that is __init__.py which contains the import statements of the modules defined in this directory.
__init__.py
from ITEmployees import getITNames 
from BPOEmployees import getBPONames 
5. Now, the directory Employees has become the package containing two python modules. Here we must notice that we must have to create __init__.py inside a directory to convert this directory to a package.
6. To use the modules defined inside the package Employees, we must have to import this in our python source file. Let's create a simple python source file at our home directory (/home) which uses the modules defined in this package.
Test.py
import Employees 
print(Employees.getNames()) 
Output:
['John', 'David', 'Nick', 'Martin']
We can have sub-packages inside the packages. We can nest the packages up to any level depending upon the application requirements.
The following image shows the directory structure of an application Library management system which contains three sub-packages as Admin, Librarian, and Student. The sub-packages contain the python modules.


Python Exceptions
An exception can be defined as an abnormal condition in a program resulting in the disruption in the flow of the program.
Whenever an exception occurs, the program halts the execution, and thus the further code is not executed. Therefore, an exception is the error which python script is unable to tackle with.
Python provides us with the way to handle the Exception so that the other part of the code can be executed without any disruption. However, if we do not handle the exception, the interpreter doesn't execute all the code that exists after the that.
Common Exceptions
A list of common exceptions that can be thrown from a normal python program is given below.
ZeroDivisionError: Occurs when a number is divided by zero.
NameError: It occurs when a name is not found. It may be local or global.
IndentationError: If incorrect indentation is given.
IOError: It occurs when Input Output operation fails.
EOFError: It occurs when the end of the file is reached, and yet operations are being performed.

Problem without handling exceptions
As we have already discussed, the exception is an abnormal condition that halts the execution of the program. Consider the following example.
Example
a = int(input("Enter a:")) 
b = int(input("Enter b:")) 
c = a/b; 
print("a/b = %d"%c) 
 
#other code: 
print("Hi I am other part of the program") 
Output:
Enter a:10
Enter b:0
Traceback (most recent call last):
  File "exception-test.py", line 3, in <module>
    c = a/b;
ZeroDivisionError: division by zero

Exception handling in python
If the python program contains suspicious code that may throw the exception, we must place that code in the try block. The try block must be followed with the except statement which contains a block of code that will be executed if there is some exception in the try block.

Syntax
try: 
    #block of code 
 
except Exception1: 
    #block of code 
 
except Exception2: 
    #block of code 
 
#other code 


We can also use the else statement with the try-except statement in which, we can place the code which will be executed in the scenario if no exception occurs in the try block.
Syntax
try: 
    #block of code 
 
except Exception1: 
    #block of code 
 
except Exception2: 
    #block of code 
 
#other code 


We can also use the else statement with the try-except statement in which, we can place the code which will be executed in the scenario if no exception occurs in the try block.
The syntax to use the else statement with the try-except statement is given below.
try: 
    #block of code 
 
except Exception1: 
    #block of code 
 
else: 
    #this code executes if no except block is executed 

Example
try: 
    a = int(input("Enter a:")) 
    b = int(input("Enter b:")) 
    c = a/b; 
    print("a/b = %d"%c) 
except Exception: 
    print("can't divide by zero") 
else: 
    print("Hi I am else block") 
Output:
Enter a:10
Enter b:2
a/b = 5
Hi I am else block
The except statement with no exception
Python provides the flexibility not to specify the name of exception with the except statement.
Consider the following example.
Example
try: 
    a = int(input("Enter a:")) 
    b = int(input("Enter b:")) 
    c = a/b; 
    print("a/b = %d"%c) 
except: 
    print("can't divide by zero") 
else: 
    print("Hi I am else block") 
Output:
Enter a:10
Enter b:0
can't divide by zero


Points to remember
Python facilitates us to not specify the exception with the except statement.
We can declare multiple exceptions in the except statement since the try block may contain the statements which throw the different type of exceptions.
We can also specify an else block along with the try-except statement which will be executed if no exception is raised in the try block.
The statements that don't throw the exception should be placed inside the else block.
Example
try: 
    #this will throw an exception if the file doesn't exist. 
    fileptr = open("file.txt","r") 
except IOError: 
    print("File not found") 
else: 
    print("The file opened successfully") 
    fileptr.close() 
Output:
File not found

Declaring multiple exceptions
The python allows us to declare the multiple exceptions with the except clause. Declaring multiple exceptions is useful in the cases where a try block throws multiple exceptions.
Syntax
try: 
    #block of code 
 
except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>) 
    #block of code 
 
else: 
    #block of code 
Example
try:   
    a=10/0;   
except ArithmeticError,StandardError:   
    print ("Arithmetic Exception")   
else:   
    print ("Successfully Done")
Output:
Arithmetic Exception

The finally block
We can use the finally block with the try block in which, we can pace the important code which must be executed before the try statement throws an exception.
The syntax to use the finally block is given below.
syntax
try: 
    # block of code 
    # this may throw an exception 
finally: 
    # block of code 
    # this will always be executed 

Example
try: 
    fileptr = open("file.txt","r")   
    try: 
        fileptr.write("Hi I am good") 
    finally: 
        fileptr.close() 
        print("file closed") 
except: 
    print("Error") 
Output:
file closed
Error

Raising exceptions
An exception can be raised by using the raise clause in python. The syntax to use the raise statement is given below.
syntax
raise Exception_class,<value> 
Points to remember
To raise an exception, raise statement is used. The exception class name follows it.
An exception can be provided with a value that can be given in the parenthesis.
To access the value "as" keyword is used. "e" is used as a reference variable which stores the value of the exception.
Example
try: 
    age = int(input("Enter the age?")) 
    if age<18: 
        raise ValueError; 
    else: 
        print("the age is valid") 
except ValueError: 
    print("The age is not valid") 
Output:
Enter the age?17
The age is not valid
Example
try: 
    a = int(input("Enter a?")) 
    b = int(input("Enter b?")) 
    if b is 0: 
        raise ArithmeticError; 
    else: 
        print("a/b = ",a/b) 
except ArithmeticError: 
    print("The value of b can't be 0") 
Output:
Enter a?10
Enter b?0
The value of b can't be 0
Custom Exception
The python allows us to create our exceptions that can be raised from the program and caught using the except clause. However, we suggest you read this section after visiting the Python object and classes.
Consider the following example.
Example
class ErrorInCode(Exception):   
    def __init__(self, data):   
        self.data = data   
    def __str__(self):   
        return repr(self.data)   
   
try:   
    raise ErrorInCode(2000)   
except ErrorInCode as ae:   
    print("Received error:", ae.data)   
Output:
Received error: 2000

Comments

Popular posts from this blog

List methods