Python 🐍 Functions And File Handling 📂

Introduction

 
Python Basics cover all the Python Datatypes and Python Tokens. After that, we learned the Python Flow Controls. Let's keep moving forward with Python Functions and Python Class/Objects and Python File Handling.
 

What is a Function?

 
A function is a block of organized, reusable sets of instructions used to perform some related actions. A function is a code that might to use again and again at some point in time.
 
Python🐍 Functions And File Handling📂
 

User-Defined Function

 
It is the function which is defined by the user.
 
Syntax
  1. def function_name(arg1, arg2, arg3,…):    
  2. statements….    
  3. return    
  4. [expression]   
Example
 
In this example: a and b are arguments and it is stored in sum function.
 
Python🐍 Functions And File Handling📂
 
Built-In Functions
 
It is the predefined set of functions in Python.
  • abs(): It returns the absolute value of a number.
  • all(): It returns true if all items in an iterable object are true.
  • any(): It returns true if any items in an iterable object are true.
  • ascii(): It returns a readable version of an object. It replaces non-ascii characters with an escape character.
  • bin(): It returns the binary versions of numbers.
  • bool(): It returns the Boolean value of specified objects.
And many more built-in functions are available.
 

How to call a Function?

 
There are two ways to call a function either by PASSING A VALUE or PASSING REFERENCE.
 

PASSING BY VALUE

 
Example
 
Python🐍 Functions And File Handling📂 
 

PASSING BY REFERENCE

 
Example
 
Python🐍 Functions And File Handling📂
 

Lambda Function

 
Lambda function is a small anonymous function of Python. It means it’s not having the name of the function. Lambda function can take any number of arguments but with only one expression.
 
Note

A lambda function cannot contain more than one expression.
 
Syntax
  1. lambda arguments: expression 
Example 
 
Python🐍 Functions And File Handling📂
 
def Functions VS Lambda Functions
 
Python🐍 Functions And File Handling📂
 
2. A normal function declared with the def keyword.
 
Anonymous functions are declared with the lambda keyword.
 
3. We can say that a+b is a normal function. On the other hand, k=a+b is the lambda function.
 

Plus Point of Lambda Function

 
An anonymous function inside a normal function,
 
Example
 
Python🐍 Functions And File Handling📂
 

Python Classes/Objects

  • Python is an object-oriented programming language.
  • Moreover, we can say that everything in Python is an object, with properties and methods.
  • A class is a “blueprint” for creating an object.
Syntax
  1. Define Class    
  2. class class_name:    
  3. define objects/methods/functions  
Example
  1. class py_class:  
  2. a=3  
  3. Call an Object:  
  4. Object_nm=class_name()  
  5. Print statement… 
Example
  1. Obj1=py_class()  
  2. print(Obj1.a) 
 
Python🐍 Functions And File Handling📂
 

File Handling in Python

 
File handling is an important part of a web application.
 
There are four features of File handling in Python,
  1. Open
  2. Read
  3. Write/Create
  4. Delete

OPEN

 
Python users can take open() to open a file. It takes two parameters, filename and mode.
 
Syntax
  1. f=open(“Path of the File”,mode) 
mode
 
There are four different types of modes,
  • “r” READ: READ mode has a default value. Opens a file for any errors in the file does not exists.
  • "a” APPEND: APPEND mode opens a file for appending, creates the file if it does not exists.
  • “w” WRITE: WRITE mode opens a file for writing, creates the file if it does not exists.
  • “x” CREATE: CREATE mode creates the specified file returns an error if the file with the same name exists
Example 
 
Python🐍 Functions And File Handling📂
 
Let’s check that filehandling.txt is created in D:/meta/ folder…
 
Python🐍 Functions And File Handling📂
 

READ

 
It is to read the contents of the file.
 
Example
 
Python🐍 Functions And File Handling📂
 
Read some parts of the file,
 
Example 
 
Python🐍 Functions And File Handling📂
 
For example, if we want to read the first 3 characters of the file:
 
Reading the first line from the file we will use the readline() function,
 
Example
 
Python🐍 Functions And File Handling📂 
 
If I want to read the first two lines of my file then I have to readline() twice.
 
Example 
 
Python🐍 Functions And File Handling📂 
 

Loop through the file

 
If I want to read my file contents line by line then I have to use a FOR Loop. 
 
Example 
 
Python🐍 Functions And File Handling📂 
 

WRITE/CREATE

 
Writing and creating this mode used when we would like to add to the file or create a new file. To write to an existing file, you must add a parameter to the open() function.
  • a-Append: will append to the end of the file.
  • w-Write: will overwrite any existing content. 
Here, the write() function is going to be used.
 
Example
 
Python🐍 Functions And File Handling📂 
 
This is for append. Now we will check with write.
 
Example
 
Python🐍 Functions And File Handling📂
 
Creating a new file
 
We can create a file using x-create, a-append or w-write modes.
  • x-Create: It creates a file, returns an error if the file already exists.
  • a-Append: Create a file if the specified file doesn’t exist.
  • w-Write: Create a file if the specified file doesn’t exist.
Example
 
Python🐍 Functions And File Handling📂 
 
f=open(“D:/meta/filehandling.txt”, “x”) This will create a file myfile.txt, otherwise, it will return an error if it already exists.
 
Python🐍 Functions And File Handling📂
 

DELETE

 
When we want to import an OS module we have to delete the file. Before deleting a file we must have to close that file. For closing a file we should use the close() function.
 
Syntax
  1. import os  
  2. os.remove(“filename with path”) 
Example
 
Python🐍 Functions And File Handling📂
 
You can see in the folder filehandling.txt is removed.
 
Note
 
Find the file attached to this article: funcs_filehandling.rar.
 
Reference Links
  • https://www.w3schools.com/python/ 
  • https://thepythonguru.com/python-lambda-function/

Summary

 
In this article, we have seen all the major functions with syntax and example with the difference of def function with a lambda function. Moreover, we have seen in detail how file handling works in Python along with its modes.


Similar Articles