#29 Python Programming – Python Function, File Dictionary and Exception Handing

python

python

Python Function

  Python functions are classified into two types

  • Built-in functions (or) library functions ( example: print(),sort(),list() etc., )
  •  User defined functions – which are created by user.

When you creating user-defined functions don’t use python keywords.

Definition

  • A function is a block of code (or) script .
  • A python function is defined as a group of statements (or) script placed inside another statement or scripts.
  • The main advantage of using function is its reusability in our working script.
  • Whenever we want to repeat a particular operation again and again, functions allow us to write the definition once and call function more than one time.

Function advantages

  • Provide reusable code /script, delivering enhanced modularity for your application.
  • Easy way to debug your script
  • Improve script scalability
  • Allows to import more than one external program
  • The Function operation block,  previously mentioned as definition block ( action block ) will use def keyword
  • Function call is nothing but to invoke a function. In python function call has bottom-up approach.

Function definition syntax

def   function_name() :

           Function definition block

Syntax rules

  • Function name must begin with keyword def
  • Function name is case sensitive .Also avoid the usage of python keywords as a function name.
  • The function definition must end with colon :
  • All other lines inside a function, placed after the function definition are indented.

How to call a function ?

Let’s start with an example function hello().

def   hello() :
         print “This is hello function block”

Calling a function definition (or) invoking a function is nothing but writing a function name, followed by open and close parentheses ( ).

hello()  # This is simple function call
print  “Exit From script”

Whenever python interprets the line hello (), the hello function block will execute and after completing the execution, the control will return back to the script section

Example :

def   hello ( ) :
              print “This is hello function block”
print “B4 function call”
hello() # simple function call
print “After function call”

Output:-

B4 function call

This is hello function block

After function call

Function definitions can be placed in any order. But its execution is based on the order of the function calls

File : p1.py

import os    #  import  python os module file into local script
def    view( ):
          print “This is view function block”
          print “display list of mounted file system details :”
          os.system(“df –Th”)  # df –Th is a unix like operating system command
          print  “Exit from view function”
 
def   display() :
         print “This is display block”
         print “Current process details”
         os.system(“ps –f”)
         print “Exit from display function”
 
print   “Im Script Section “
display()  # 1st call  
view()  # 2nd call
print “Exit From script”

Nested function calls

  • Nested function calls are similar to nested structures where the function calls are placed inside another function.
  • These nested structures are applied in many situations which includes dependency-based actions and stack applications.
  • Let’s have an example
def  functionA() :
           print  “Im functionA”
           functionB() # nested call
 
def functionB():
          print “Im functionB”
          functionC()  # nested call
 
def functionC():
          print “Im functionC”
 
functionA()  # This is main script call
print “Exit from function block”

Output :-

Im functionA

Im functionB

Im functionC

Exit From function block

Function call with arguments

While passing arguments to a function, those arguments should be placed in the correct positional order.

def  hello(a1,a2) :  
          print “1st argument :”,a1
          print “2nd argument:”,a2
          print “Arguments types are :”
          print type(a1)
          print type(a2)


hello(10,20)                                       # arguments are numbers
hello(“/etc/passwd”, “/var/log/auth.log”)   # arguments are string
hello(“data1”,”data2”)                             # arguments are string
hello([‘one’,’two’,’three’],(“three”,”four”))         # arguments are list and tuple
hello({‘key1’:’Value1’,’key2’:’Value2’}, [10,20,30]) # arguments are dict and list

In python, the arguments passed inside a function are classified as

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required arguments

  •  These are the function arguments that have to be passed to a function in correct positional order.
  • The order of the arguments should match in both function call and function definition.
  • In case of any mismatch, the python return an error (Type Error) message and exits from the script without continuing to the next statement.

File : p1.py

print "I am python"
def a():  #  No arguments are given
          print "This is a block"
a(10,20)  #  call with two arguments
for v in [1,2,3,4,5]:
          print v
print "Exit from block"

Output :

I am python

Traceback (most recent call last):

  File “a.py”, line 5, in

    a(10,20)

TypeError: a() takes no arguments (2 given)

  • In the above example, the control stops executing the next looping statements placed after the function call.
  • Using exception handling technique we can handle this exception and can make the control move forward to the next statement of execution.
  • Here, the number of arguments (count) in the function call should also match exactly with the number of arguments in function definition.
def   display(a1,a2):

          print “This is display block”

          print “Received 1st argument:”,type(a1)

          print “Received 2nd argument:”,type(a2)

# The above code display block contains two arguments

display(“data1”,”data2”)  # valid call
display() # empty argument – invalid call
display(10,20,30) #more than two arguments - invalid call

Keyword  arguments

  •  Keyword arguments are related to the function calls.
  • When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
  • This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.
def  display( name,age) :
          print  “Name:”, name
          print “Age:”,age
 
display(name=”Mr.Xerox”,age=55) 

In python, the function will take the default value if the value is not passed in the function call as an argument.

def display(name=”Mr.Xerox”,dept=”Sales”) :
                   print “Name:”,name
                   print “Dept:”,dept
display()
display(name=”Mr.John”,dept=”Account”)
display(name=”Mr.Paul”)
display(dept=”Production”)
  • A default argument is an argument that assumes a default value if the value is not provided in the function call as argument.

Variable-length arguments

  • You may need to process a function for more arguments than you specify while defining the function.
  • These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.
def  functionname(*variable) :
                   function definition operation
 
functionname(arg1,arg2,arg3……argN)

Example

def  display(a1,*a2):

          print type(a1)  #  type ‘str’

          print type(a2)  #  type `tuple`

display(“data”,10,20,30,40,50,60)

  • An asterisk (*) is placed before the variable name that holds the values of all non-keyword variable arguments.
  • These collection of non-keyword arguments form a tuple and it remains empty if no additional arguments are specified during the function call.

The *args vs **kwargs

  •  We studied  *args allows you to do is take in more arguments than the number of formal arguments that you previously defined.
  •   With *args, any number of extra arguments can be treated as tuple type in formal parameters 
  •  The special syntax**kwargs in function definitions in python is used to pass a keyword variable-length argument list.
  • We use the name kwargs with the double star.
  •  The reason is because the double star allows us to pass through       keyword arguments (and any number of them).
  •  A keyword argument is where you provide a name to the variable as you pass it into the function.
  •  The kwargs as being a dictionary that maps each keyword to the value that we pass along it.
  • That’s the reason when we iterate over the kwargs there doesn’t seem to be ordered result.
  •  kwargsreturn type will be ‘dict’ type.
>>> def    display(**kwargs):
...             print type(kwargs)

...

>>> display(name="Karthik",place="Bangalore")

 type 'dict'

>>> def    display(**kwargs):

 

...     for v in kwargs.keys():

...             print v,"\t",kwargs[v]

>>> display(name="Karthik",place="Bangalore")

#output
place   Bangalore

name    Karthik

Anonymous function

  •  Anonymous function (or) lambda function i.e., functions unnamed or without name.
  • The general syntax of a lambda function:-

lambda argument_list expression

  • The argument_ list consists of list of arguments separated by comma and the expression is
  • an arithmetic expression using these arguments.
  • You can assign the function to a variable to give it a name.

>>> f = lambda x, y : x + y

>>> f(10,20)

30

Scope of variables

  • All variables within a program may not be accessible at all locations in that program.
  • This depends on where you have declared that variable.
  • The scope of a variable determines the portion of the program where you can access a particular identifier.
  • There are two basic scopes of variables in Python ?
  • Global variables
  • Local variables
  • Variables that are defined inside a function is called local variables
  • Local variables can be accessed only inside the function, we can’t access outside the function

Example:

def    hello():
         Fname=”/var/log/result.log”   # local variable
         print “File name:”,Fname  
hello() # function call
print “Outside function  File name:”, Fname  ## NameError
  • We can use a global variable in other functions by declaring it as global in each function that assigns to it.
  • Global variables are accessible from anywhere in the script  i.e., inside a function and outside a function.

Example :

def  hello():
         global Fname #  variable Fname is a global variable
         Fname=”/var/log/result.log”   
 
def  display():
       print “Result log file name is: “,Fname   # Result log file name is:/var/log/result.log
 
hello()
display()
print  “Outside function  File name:”  # display Outside function File name:/var/log/result.log

File Handling categories

  • Read a data from Keyboard(STDIN) to script write a data to FILE ( Not using Monitor(STDOUT) )
  • Read a data from FILE to Script display to Monitor ( Not using STDIN )
  • Read a data from one FILE to Script write a data to another FILE (Not using STDIN/STDOUT. (like: cp old new))
  • When we want to read from or write to a file we need to open it first.
  • When we are done, it needs to be closed, so that resources that are tied with the file are freed.
  • Hence, in Python, a file operation takes place in the following order.
    • Open a file
    • Read or write (perform operation)
    • Close the file

How to open a file?

  • Python has a built-in function open() to open a file.
  • This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.

Syntax :

Object=open(“Inputfile”)
>>> f = open("test.txt")    # open file in current directory
>>> f = open("/var/log/temp.log")  # specifying full path
  • We can specify the mode while opening a file.
  • In mode, we specify whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file.
  • We also specify if we want to open the file in text mode or binary mode.
  • The default is reading in text mode. In this mode, we get strings when reading from the file.
  • On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Python File Modes

Python File Modes
ModeDescription
‘r’Open a file for reading. (default)
‘w’Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
‘x’Open a file for exclusive creation. If the file already exists, the operation fails.
‘a’Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
‘t’Open in text mode. (default)
‘b’Open in binary mode.
‘+’Open a file for updating (reading and writing)

Example:-

f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode

How to close a file Using Python?

  • When we are done with operations to the file, we need to properly close the file.
  • Closing a file will free up the resources that were tied with the file and is done using Python close() method.
  • Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.
f = open("test.txt)
# perform file operations
f.close()

How to read a file using python ?

Syntax :-

Object=Open(“Inputfile”,”mode”)
Example:-
>>> obj=open(“/var/log/result.log”,”r”)  # r – read mode by default.
>>> type(obj)

Read data from file in python we are using different functions.

1.read() – read the entire file contents and returns them as a single string
Example :-

cat IP1.txt
Data1
Data2
Data3
Data4
obj=open(“IP1.txt”)
obj.read()
‘Data1\nData2\nData3\nData4\n’
type(obj.read())

2. readline() – read the file contents line by line return as a single string

>>> obj=open(“IP1.txt”)
>>> obj.readline()
‘Data1’
>>> obj.readline()
‘Data2’
>>> obj.readline()
‘Data3’
>>> obj.readline()
‘Data4’
>>> obj.readline()
“ ”

3. readlines() – read the entire file contents and return as a single list type of data.

>>>obj=open(“IP1.txt”)
>>>type(obj.readlines())
>>>obj.readlines()
[‘Data1\n’,’Data2\n’,’Data3\n’,’Data4\n’]

4. next() – read line by line like readline() and return the next item from the iterator.

  • If default is given and the iterator is exhausted , control returns instead of raising StopIteration
>>>obj=open(“IP1.txt”)
>>>type(next(obj))
>>> next(obj)
‘Data1’
>>> next(obj)
 ‘Data2’
>>> next(obj)
 ‘Data3’
>>> next(obj)
 ‘Data4’
>>> next(obj)
StopIteration 
  •  Note that above object (obj) is iterable object. So will use for loop will read data from file.
>>> obj=open(“IP.txt”) # open a file , bydefault read mode
>>> for var in obj:  # iterable object
              print var.strip() # strip() function is used to remove \n char
>>> obj.close()  # close the file object
     Data1
     Data2
     Data3
     Data4
     Data5
  • This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.
  •  A safer way is to use exception handling techniques which we will be discussing later. 
try:
       f = open(“test.txt”)
except Exception as e:
     print e
else:
      for var in f:
               print var.strip
finally:
       f.close()

How to write to a File in Python?

  • In order to write into a file in Python, we need to open it in write ‘w’, append ‘a.

Syntax:-

open(“Filename”,” w”)

Example:-

f=open("test.txt",'w’):
f.write("my first file\n")
f.write("This file\n\n")
f.write("contains three lines\n")
f.close()
  • This program will create a new file named ‘test.txt’ if it does not exist. If it does exist, it is overwritten.
  • We must include the newline characters ourselves to distinguish different lines.

Using with keyword

with open("test.txt",'w') as f:
        f.write("my first file\n")
        f.write("This file\n\n")
        f.write("contains three lines\n")
  •   This program will create a new file named ‘test.txt’ if it does not exist.
  •   If it does exist, it is overwritten.
  •   When using with keyword, file handler will be closed automatically.
  •  No need to explicitly use f.close().  

Using seek() method

We can change our current file cursor (position) using the seek() method.

>>> obj.seek(0)   # brings file cursor to initial position
0
>>>obj=open(“IP.txt”)
>>>obj.read()
‘Data1\nData2\nData3\nData4\nData5\n’
>>>obj.read()
“ “ # empty string
>>>obj.seek(0)  # brings file cursor to initial position
>>>obj.read()
‘Data1\nData2\nData3\nData4\nData5\n’

Python Directory Handling

  •  A directory or folder is a collection of files and sub directories.
  •  Using python os module we can handle the directories

Get Current Directory

  •   We can get the present working directory using the getcwd()method.
  •   This method returns the current working directory in the form of a string
>>> import os
>>> os.getcwd()
'C:\\Users\\Krosumlabs\\Desktop'

Changing Directory

  • We can change the current working directory using the chdir() method.
>>> os.chdir('C:\\Python33')
>>> print(os.getcwd())
C:\Python33
  •   The new path that we want to change must be supplied as a string argument to this method.
  •   We can use both forward slash (/) and the backward slash (\) to separate the path elements.

List Directories and Files

All files and sub directories inside a directory can be known using the listdir() method.

>>> os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']

Making a New Directory

  •  We can make a new directory using the mkdir() method.
  •  This method takes in the path of the new directory. If the full path is not specified the new directory is created in the current working directory.
>>> os.mkdir('test')
>>> os.listdir()
['test']

Renaming a Directory or a File

  • The rename() method can rename a directory or a file.
  • The first argument is the old name and the new name must be supplied as the second argument.
>>> os.listdir()
['test']
>>> os.rename('test','new_one')
>>> os.listdir()
['new_one']

Removing Directory or File

  • A file can be removed (deleted) using the remove() method.
  • Similarly, the rmdir() method removes an empty directory.
>>> os.listdir()
['new_one', 'old.txt']
 
>>> os.remove('old.txt')
>>> os.listdir()
['new_one']
 
>>> os.rmdir('new_one')
>>> os.listdir()
[]
  •  However, note that rmdir() method can only removes empty directories.
  •  In order to remove a non-empty directory we can use the rmtree() method inside the shutil module.
>>> os.listdir()
['test']
>>> os.rmdir('test')
Traceback (most recent call last):
...
OSError: [WinError 145] The directory is not empty: 'test'
 
>>> import shutil
>>> shutil.rmtree('test')
>>> os.listdir()
[]

Multidimensional data structures

Combination of List,tuple and dictionary structures

List  of List

>>> confs=[["p1.log","p2.log","p3.log"],["host01","host02"],
                          ["/etc",  ["passwd",”gpasswd"] ]  ]
>>> type(confs)
>>> confs[0]  # display 0th index
['p1.log', 'p2.log', 'p3.log']
>>> confs[1]  # display 1st index
['host01', 'host02']
>>> confs[2] # display 2nd index
['/etc', ['passwd', 'gpasswd']]
>>> confs[2][1] # list of list 2nd index of 1st index
['passwd', 'gpasswd']
>>>
>>> confs[1] # 1st index
['host01', 'host02']
>>> confs[2][1]
['passwd', 'gpasswd']
>>> confs[2][1][0] # list of list
'passwd'

List of tuple

# List inside tuples
>>> IPs=[('host01','host02','host03'),('host04',('host05'))]
>>> type(IPs)
>>> IPs[0]
('host01', 'host02', 'host03')
>>> type(IPs[0])
>>> IPs[-1]
('host04', 'host05')
>>> IPs[-1][0]
'host04'
>>> IPs[-1][1]
'host05'

Tuple of Tuple

# Read only mode
>>> files=("/etc/passwd",("p1.log","p2.log"),("p3.log"),("p4.log"))
>>>
>>> type(files)
>>>
>>> type(files[1])
>>> files[1]
('p1.log', 'p2.log')
>>>
>>> files[1][0]
'p1.log'

Tuple of List

>>> IPs=(['10.20.30.40','10.20.30.50','10.20.30.65'],['/etc/passwd','/etc/hosts','/etc/gpasswd'])  # tuple of list
>>> type(IPs)
>>> IPs[0]  # 0th index is List
['10.20.30.40', '10.20.30.50', '10.20.30.65']
>>> type(IPs[0]) # List type
>>> IPs[0][0] # tuple of list
'10.20.30.40'
>>> IPs[0][1]
'10.20.30.50'
>>> IPs[1]
['/etc/passwd', '/etc/hosts', '/etc/gpasswd']
>>> IPs[1][-1]
'/etc/gpasswd'
>>> IPs[-1]
['/etc/passwd', '/etc/hosts', '/etc/gpasswd']

Dictionary of List

# Single key points to multiple values ( list )
 Confs={'hosts':['/etc/hosts','/etc/hostname','/etc/sysconfig/network-scripts’]}
# ‘hosts’ is a key – value is list
>>> type(Confs)
>>> Confs['hosts']
['/etc/hosts', '/etc/hostname', '/etc/sysconfig/network-scripts']
>>> type(Confs['hosts'])
>>> Confs['hosts'][0]  # from dict key is pointing to 0th index list
'/etc/hosts'
>>> Confs['hosts'][-1] # from dict key is pointing to last index
'/etc/sysconfig/network-scripts'

Dictionary of tuple

# Dictionary key  value is tuple
 Confs={'hosts':('/etc/hosts','/etc/hostname','/etc/sysconfig/network-scripts’)}
>>> type(Confs)
 
>>> Confs['hosts'] # dictionary key is  tuple value
('/etc/hosts', '/etc/hostname', '/etc/sysconfig/network-scripts')
 
>>> type(Confs['hosts'])
>>> Confs['hosts'][0]  #  0th index
'/etc/hosts'
>>> Confs['hosts'][1]
'/etc/hostname'
>>> Confs['hosts'][2]
'/etc/sysconfig/network-scripts'

Dictionary of Dictionary

# Dictionary is holding another dictionary
>>> Confs={'host1':{'file1':'/etc/hosts'},'host2':{'file2':'/etc/hostname'},
                      'host3':{'file3':'/etc/sysconfig/network-scripts'}}
>>> type(Confs)
>>> type(Confs['host1'])  
>>> Confs['host1'] # host1 key value is dictionary
{'file1': '/etc/hosts'}
>>> Confs['host1']['file1']  # dictionary of another dictionary
'/etc/hosts'

Exception Handling in Python

Exceptions

  • An exception is a signal that an error or other unusual condition has occurred.
  • When that error occurs, Python generate an exception that can be handled, which avoids your program to crash.
  • Exceptions are convenient in many ways for handling errors
  • Below is some examples explaining some common exceptions errors in Python:

Examples

>>> var=10
>>> print Var  # Invalid variable –python case sensitive
Traceback (most recent call last):
  File "", line 1, in
NameError: name 'Var' is not defined
>>> a=['one','two','three']
>>> a[3] # invalid index
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
 
>>> import Http  # module is not loaded
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named Http
# Temp.log file is not exists
>>> open("Temp.log")
Traceback (most recent call last):
  File "", line 1, in
IOError: [Errno 2] No such file or directory: 'Temp.log'

Handling an exception

  • In basic terminology we are aware of try/except clause.
  • The code which can cause an exception to occur is put in the try block and the handling of the exception is implemented in the except block.

Syntax

try:

    operation

except Exception :

    Handle the exception

Example


NameError: name 'Var' is not defined
>>> try:
...     Var # Exception is occured here
... except NameError:
...     print "Invalid variable"
...
Invalid variable
>>>try:
…     print 1/0
…except ZeroDivisionError:
       print "You can't divide by zero."
  •  The error handling is done through the use of exceptions that are caught in try blocks and handled in except blocks.
  •  If an error is encountered, a try block code execution is stopped and transferred down to the except block.

Handling multiple exceptions

Syntax:-

try:
          operation
 except Exception1:
           Handle the exception
 except Exception2:
           Handle the exception
 except Exception3:
           Handle the exception
 .......
except Exception'N':
           Handle the exception
 
Example:-
>>> try:
...     open("Temp.log") # File is not available
... except NameError:
...     print "Invalid variable"
... except IOError:
...     print "File is not exists"
... except ZeroDivisonError:
...     print "Divide by zero"
...
File is not exists

Try … except … else clause

  • The else clause in a try/ except statement must follow all except clauses, and is useful for the code that must be executed if the try clause does not raise an exception.
try:
        operation
except Exception:
        handle_the_exception_error
else:
         # follows the code that there is no exception    
Example :-
>>> try :
...     var=10
... except NameError:
...     print "Name error"
... else:
...     print "No Exception"
...     print "var value is:",var
No Exception
var value is: 10

Try … finally clause

  • The finally clause is optional.
  • It is intended to define clean-up actions that must be executed under all circumstances.
  • Whether the exception occurs or not, the finally block will always execute.
  • Make sure that the else clause is run before the finally block.

Example:

File : e1.py

try:
           obj=open("Temp.log")
except IOError:
           print "Invalid file"
else:
           print "Read operation"
            for var in obj:
                     print var
                     obj.close
finally:
            print "-----Thank you----"
  •  Let Temp.log file does not exist. So the exception occurs and the control moves to the except clause.
  •   It displays ‘invalid file’ message and then will go to finally block displaying a ‘—-Thankyou —-‘.

C:\Users\Krosumlabs\Desktop>python e1.py

Invalid file

—–Thank you—-

Now create a Temp.log file and then run the Script discussed above.

Now, there is no exception. So the control goes to else clause, followed by the finally block.

C:\Users\Krosumlabs\Desktop>python e1.py

Read operation

dat1

data2

—–Thank you—-

A finally clause is always executed before leaving the try statement, irrespective of whether an exception has occurred or not.

Raising an Exception

  • In Python programming, exceptions are raised when corresponding errors occur at run time, but we can forcefully raise it using the keyword raise.
  • We can also optionally pass values to the exception to clarify why that exception was raised.
>>> raise KeyboardInterrupt
Traceback (most recent call last):
KeyboardInterrupt
>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
MemoryError: This is an argument
>>> try:
...     a = int(input("Enter a positive integer: "))
...     if a <= 0:
...         raise ValueError("That is not a positive number!")
... except ValueError as ve:
...     print(ve)
...    
Enter a positive integer: -2
That is not a positive number!

Python Built-in Exceptions

  •   Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.
  •    We can view all the built-in exceptions using the local() built-in functions as follows.

>>> locals()[‘__builtins__’]

  •   This will return us a dictionary of built-in exceptions, functions and attributes.

Reference

https://www.python.org/

https://www.anaconda.com/

Continue exploring at Teknonauts.com

Leave a Reply

Your email address will not be published. Required fields are marked *