Skip to content

File Handling in Python – An intro for Beginners

File handling in Python

In this tutorial we will learn about File handling in Python . So far we have learned about standard input and standard output. Now we will learn how to use .txt / .bin file. Python provides some basic functions and methods through which we can control any .txt /.bin file.

What is File pointer ?

File pointer is a blinking position, which is at the very beginning of the file when it is opened. But when we start reading file content or start writing in file, it also starts moving.

Python open() function | How to open a file in Python ?

Before reading or writing into a file, we have to open the file. We will use the open function to open a file. The open function returns a file_object (filepointer).

Syntax of open() function

open(file location , access mode)

Parameters of open() function

file location = If the file is in this folder where we are working now then using only filename will work like “hello.txt” otherwise we have to use full file-path like “C:\Users\username\Desktop\hello.txt “

access mode = Optional , Access mode specifies that mode in which we want to open any file . Python usually offers three types of access mode. These are “r” (for reading the file), “w” and “a” (for writing in the file). Default access mode is “r” . Below are all the access modes expressed in the form of a list –

  • r mode = Opens the .txt file in read-only mode. File pointer is in 0th position when opening. If we do not enter anything in access mode when opening the file, then it automatically takes “r” access mode. “r” mode and “rt” mode refer to the same mode.
  • rb mode = Opens the .bin file in read-only mode .
  • r+ mode = If we open the .txt file in this mode, we can read the file and write into the file. File pointer is in 0th position when opening.
  • rb+ mode = Same as “r+” mode but works with .bin file .
  • w mode = Opens the .txt file in write-only mode. If the file exists then delete the text of the file and rewrite it, otherwise create a new file.
  • wb mode = Same as “w” mode but works with .bin file .
  • w+ mode = In this mode, if we open the .txt file, we can read the file, also write into the file. If the file exists then delete the text of the file and rewrite it, otherwise create a new file.
  • wb+ mode = Same as “w+” mode but works with .bin file .
  • a mode = Opens the .txt file in write-only mode. If the file exists then adds new text at the end of the file, otherwise it creates a new file.
  • ab mode = Same as “a” mode but works with .bin file .
  • a+ mode = In this mode, if we open the .txt file, we can read the file, also write into the file. If the file exists then adds new text at the end of the file, otherwise it creates a new file.
  • ab+ mode = Same as “a+” mode but works with .bin file .

Python File close() method | How to close a File in Python ?

When the work for which we opened the file is done, we should close the file, otherwise it will occupy space in the ram which may cause our system to slow down. Also, if we do not close the file, we will not be able to see the changes we made to the file. We will use the close() method to close the file.

Syntax of close() method

file_object.close()

Parameters of close() method

This method does not have any parameter .

File object attributes in Python

After opening a file, when we get the file object, we can get different information about the file with the help of different attributes of the file object. Below are the different attribute of the file object –

  • file.closed = If the file is closed then it will return True else False.
  • file.mode = Returns the access mode in which mode the file is opened .
  • file.name = Returns the name of the file .
  • file.encoding = Returns the encoding the file is used .

Here we will use the file “hello.txt” and before seeing the examples of file first we will keep the file in the current folder where we are working now .

Here is the content of hello.txt –

Hello, World1!
Hello, World2!
Hello, World3!
Hello, World4!
Hello, World5!

Examples of file handling in Python

Example 1:

# opening the hello.txt file
fp=open("hello.txt")

# checking if the file closed or not
print("file closed =",fp.closed)

# checking the file name
print("file name =",fp.name)

# checking the mode
print("file mode =",fp.mode)

# checking the encoding
print("file encoding =",fp.encoding)

# closing the file
fp.close()

# checking if the file closed or not
print("file closed =",fp.closed)
file closed = False
file name = hello.txt
file mode = r
file encoding = cp1252
file closed = True
  • In the above code first we have opened a file named “hello.txt” . As we did not specify access mode so the file is opened in “r” access mode .
  • Then we have checked if the file is open or not using closed attribute . As the file is open so it has returned False .
  • After that we have checked the name of the file using name attribute . From the output we can see that file name is “hello.txt” .
  • Then we have checked the access mode of the file using mode attribute . From the output we can see that access mode is “r” .
  • After that we have checked the encoding of the file using encoding attribute . From the output we can see that encoding is “cp1252” .
  • Then we have closed the file .
  • After closing the file when we have checked if the file is open or not using closed attribute it has returned True .

Opening a file in Python using with keyword

If we open the file with the keyword with then when the program exits the with block then the file will close automatically, we do not have to apply the file.close() method.

Example 1:

# opening a file using with keyword
with open("hello.txt") as fp:
    # checking if the file is closed or not    
    print("file closed =",fp.closed)
    
# checking if the file is closed or not    
print("file closed =",fp.closed)
file closed = False
file closed = True
  • In the above code first we have opened “hello.txt” file using with keyword .
  • Then when we have checked if the file is closed or not within the with block it has returned False .
  • .After that we have again checked if the file is closed or not after exiting from with block . As we know if we exit from with block file will close automatically so it has returned True .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply