/  Technology   /  Python   /  File Handling in Python
FILE HANDLING IN PYTHON (i2tutorials)

File Handling in Python

Python supports file handling and allows us to handle files like read, write files along with other file handling options to operate on files. Python treats the file as text or binary. Every line of code includes a sequence of characters and forms as a text files where each line of the file is ended with a special character called End of line characters (EOL) like comma or new line character. It terminates current line ad interprets a new one. 

open()

open() function is used to open a file in python in order to read or write. open() function returns a file object. It has two arguments file name and mode whether to read or write. 

Syntax:

open(filename, mode)

In order to open a file, there are different methods or modes.

If we have to open a file and read then read mode is used. This mode is represented by “r”. If file does not exist, it returns error.

If we have to append a file, then append mode is used. If such file does not exist, it will create a new file with same name. This mode is represented by “a”.

Writing to a file is also somewhat similar to appending a file. It also creates a new file if that file does not exist. In order to open a file and write to it write mode is used. It is represented by “w”.

For creating a new file “x” mode is used. If already file exist with same name, then it returns error as file already exists.

We can also specify the file whether it should be in text mode or binary mode.

For text mode, “t” is used and for binary mode, “b” is used.

Python Code:

open(“filename.txt”, “r”)

read()

Apart from the above method, we can also read a file in python by using other method. If we want to extract a string in the file, then file.read() method can be used.

Python Code:

file = open("file.txt", "r")  
print file.read() 

write()

As discussed above, if file already exists, we can write to the file, or it will create a new file and can be able to write to that file. This can be done by the function write().

Syntax:

open(“filename.txt”, ”w”)

Python Code:

File= open(“filename.txt”, ”w”)
File.write(“This is the new statement”)
File.close()

close() command terminates all resources and frees the system of this specific program.

append()

This is similar to write mode, only difference is that it adds the new text to the already existing text without erasing it.

Syntax:

open(“filename.txt”, “a”)

Python Code:

file = open(“filename.txt”, “a”) 
file.write("This will add new line") 
file.close() 

Opening a file by using “with”

We can also open a file by using with() function. Syntax of this method along with write() is given below.

Python Code:

with open(“filename.txt”, “r”) as file:
	f.write(“file handling in python”)

split()

In python, we can also perform splitting by using file handling. This function splits the variable when space is introduced. We can also split by using any other characters.

Python Code:

with open(“filename.txt”, “r”) as file:
	text= file.readlines()
	for line in text:
	word=line.split()
 	print(word)

Leave a comment