All Courses

WHAT IS FILE MANAGEMENT IN PYTHON.

Sulochana Kamshetty

2 years ago

FILE MANAGEMENT IN PYTHON
Table of Content
  • Evolution of File Management
  • Creating a file using write() mode
  • Python code to create a file
  • Working of append() mode
  • Output
  • Python code to illustrate with() along with write()
  • Output
  • Python code to illustrate split() function
  • Output
       In this article, we will try to learn about a new topic i.e., WHAT IS FILE MANAGEMENT IN PYTHON.
For adding the more informative knowledge of python world, understanding about file management in python will also take some examples.

In Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing, and reading files  
Come Let us understand the key points of why one has to know about
1.       File management in python,
2.       Uses and its importance of file management,
3.       Where and when file management is used in real-time.

Evolution of File Management:

         The first management system introduced in 1960’s. This was actually the proof of concept, in which the feasibility of storing large amount of data was shown. It’s the management system,, in which all data is stored on a single large file as indicated from its name.
having an effective computer filing system, can make a huge difference in your everyday life. Firstly, it helps to save time, but most importantly, it helps you to be more organized
We use open () function in Python to open a file in read or write mode.
As explained above, open ( ) will return a file object. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write. So, the syntax being: open(filename, mode). There are three kinds of mode, that Python provides and how files can be opened:
Not only read and write the file we even can do multiple task like:
1.    Creating a file using write() mode
2.    Working of append() mode
3.    Using write along with the () function
4.     split() using file handling
Now lets understand the important aspects of file management those are
 r “, for reading.
“ w “, for writing.
“ “, for appending.
“ r+ “, for both reading and writing
Lets look about the coding part now
#  a file named
  "insideaiml", will be opened with the reading mode.

file = open('inisdeaaiml.txt', 'r')
Output:- This will open the file which is stored as insideaiml.txt in reading mode 
This will print every line one by one in the file
for each in file:
print (each)
There are more than one way to read a file in Python. If you need  to extract a string that contains all characters in the file then we can use file.read(). The full code would work like this:
Python code to illustrate read() mode
file = open("insideaiml.text", "r")

print (file)
Output:- this command will help us to open the file with the help of reading mode & print option will help us to open the file in reading mode 
Another way to read a file is to call a certain number of characters like in the following code the interpreter will read the first five characters of stored data and return it as a string:
  • filter_none
  • edit
  •  play_arrow
  • brightness_4
Python code to illustrate read() mode character wise
file = open("file.txt","r")

print(file.read(5))
Output:-
The open command will open the file in the read mode and the for loop will print each line present in the file.

Creating a file using write() mode

Let’s see how to create a file and how to write mode works:
To manipulate the file, write the following in your Python environment:
Python code to create a file
file = open('insideaiml.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()
Output:- The close() command terminates all the resources in use and frees the system of this particular program.
Working of append() mode
Let’s see how the append mode works:
Python code to illustrate append() mode
file = open('insideaiml.txt','a')
file.write("This will add this line")
file.close()
Output:- So append function helps in adding the additional information even after creating the code to the existing one
now lets check with with()function
Using write along with with() function
Python code to illustrate with() along with write()
with open("file.txt", "w") as f:

f.write("Hello World!!!")

split() using file handling
Output:- this function  helps us and concatenating the new information to the existed created file.
let us understand how to use split functions works in python…
We can also split lines using file handling in Python. This splits the variable when space is encountered. You can also split using any characters as we wish. Here is the code:
Python code to illustrate split() function
with open("file.text", "r") as file:

data = file.readlines()

for line in data:

word = line.split()

print word
Output:-
       There are also various other functions that help to manipulate the files and its contents. One can explore various other functions in Python Docs.
There are also various other commands in file handling that is used to handle various tasks like:
  • rstrip(): This function strips each line of a file off spaces from the right-hand side.
  • lstrip(): This function strips each line of a file off spaces from the left-hand side.
It is designed to provide much cleaner syntax and exceptions handling when you are working with code. That explains why it’s good practice to use them with a statement where applicable. This is helpful because using this method any files opened will be closed automatically after one is done, so auto-cleanup.
I hope you enjoyed reading this article and finally, you came to know about What is File Management in Python. 
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review