File Handling & Modules

File Handling & Modules

Functions

Python function is a named block of code that performs a specific task.

Let's see an example of creating a function.

The type() says that example() is a function. After executing the function:

Another example of writing a function to join two strings.

File Handling Operations

File handling operations refer to tasks or operations performed on files like reading and writing data to files, appending files and closing files.

The pointer is the location where you currently start to read or write.

The read() function is used to read the entire content of the file.

After operating it is mandatory to use the close() function to reflect all the changes we made to the file.

The write() function is used to write or append data to the file.

One important point to note is that when you use the write function it starts to write from the beginning i.e. the first character itself thus overriding all the previously present data.

The append() method is used to write or add data to the end of the file. This method doesn't override the previously existing data.

Modules In Python

A module is a file that contains Python code containing functions, classes, variables etc which can be used in other Python programs by importing those modules.

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!