File Handling In Python πŸ˜€

In this article, we are going to learn about file handling in Python.

File Handling in Python 

Python also supports file management and allows users to manipulate files, i.e;  Read and write files, along with many other file handling options to work with files.

The concept of file management has been extended to several other languages, but the implementation is complicated or lengthy, but like other Python concepts, this one is simple and short. Python treats files differently as text or binary, and that's important. Each line of code contains a sequence of characters and forms a text file. Each line of a file ends with a special character called End Of Line characters like comma {,} or newline character. Ends the current line and tells the interpreter that a new line has started. 

Before performing any operations on the file like reading or writing, we need to open this file first. For this, we need to use Python's built-in open() function, but at the time of opening we need to specify the mode that represents the purpose of the opening file.

Let's learn about working of open() function.

f= open(filename,mode)

Here is the list of following mode -

  • r: Open an existing file for reading operations.
  • w: Open an existing file for writing operations. If the file already contains data, it will be overridden.
  • a:  Open an existing file to append. Existing data will not be overridden.
  •  r+:  For reading and writing data to the file. Any previous data in the file will be overridden.
  • w+: For writing and reading data. Existing data will be overridden.
  • a+: For appending and reading data from the file. Existing data will not be overridden.

Let's look into example,

# a file named "sample", will be opened with the reading mode.
file = open('sample.txt', 'r')
# This will print every line one by one in the file
for each in file:
Β Β  Β print (each)

The open command opens the file in read mode only.

Let's learn about working of read() function

There are several ways to read a file in Python. If we need to extract a string containing all characters from the file, we can use file.read(). The full code would work like this:

# Python code to illustrate read() operation
file = open("sample.txt", "r")
print (file.read())

Let's learn about working of write() function

Let's see how a file is created and how the write mode works. So to manipulate the file, write the following in your Python environment:

# Python code to create a file
file = open('sample.txt','w')
file.write("This is the write command")
file.write("It allows us to write operations in a particular file")
file.close()

The close() command terminates all used resources and clears the system from that particular program.

Let's learn about working of split() function

We can also split lines using Python file handling. This will split the variable when a space is found. You can also share with any character. 

# Python code to illustrate split() function
with open("sample.text", "r") as file:
	data = file.readlines()
	for lines in data:
		words = lines.split()
		print (words)

There are also several other functions to help you manipulate the files and their contents.

Happy Learning :)


Similar Articles