Editor choice

Open files in python | python file handling | object handling

To open a file in python

we need the following syntax


<file_objectname> = open(<filename>)
<file_objectname) = open(<filename>, <mode>)


For example, 


And also we have seen different modes of opening a file according to our needs



file2 = open("data.txt","r")

file3 = open("e:\\main\\result.txt", "W")

In the first line,

we are opening file “mypython.txt” in file mode as reading mode also known as the default mode

and connects it to the file object that we named as file1.

the second line,

we open the file “data.txt” in the reading mode because ‘r’ is given as mode

and attach it to the file object namely file2.

In the third line,

we open file “result.txt” which stored in folder E:\ main directory in write mode as we can see that we have given mode as “w”

and associates it to file object namely file3.

What is file object in python?

What is file mode?

to support answers to these questions we should know about the open file function in more detail

Python’s open() function creates a file object which serves as a link to a file staying on our


The first parameter

The first parameter for the open() function should given as its path to the file we would like to open.

If only the file name given, then Python searches for the file in the current folder

The Second parameter

The second parameter of the open() function resembles a mode which typically read (r), write (‘w’), or 

append (‘a’).

If we didn’t give a second parameter, then with the default it opens it in read (‘r’) mode.

Important points to be remember

Please note that when we open a file in read mode,

the given file must exist in the folder, otherwise Python will put FileNotFoundError.

The prefix r in front of a string makes it a raw string which means.

There exists no special meaning assigned to any character.

The default file-open mode in read mode,

i.e., if we do not provide any file open mode,

Python will open it in read mode (“r”) by default.

If we just give the file name without its path for example just “data.txt” instead of “c :\\temp\\data.txt” ,

Python will open or create the file in the same directory in which the module file stored.

File object handle:

File objects are related to read and write data to a file on a disk.

The file object is used to obtain a reference to the file on the disk stored.

and open it for a number of different tasks.

File object means also called file-handle which implies a very important and useful tool as by a file-object only,

a Python program can work with files stored on the hardware.

All the functions that we perform on a data file are performed through file objects.

When we use file open(),

Python stores the reference of the specified files in the file object. A file object of Python denotes a

collection of bytes where the data could be read both bytes by byte or line by line or collectively.

All this will be clear to yourself when you CLICK HERE

We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?