Open File in Python | File Handling in Python Programming
In our previous topic, we have seen and studied about need of file handling in python. So in this session we will learn how to Open and Create a file.
1). Open File: There are three types of files ie. text file, binary files and csv files. We will talk about text file. So, to perform any operation on input/output data and to store that data in file, we need to create a file first in which we can store that data.
We will discuss how to open a file first, later we will talk about how to create a file.
Syntax 1:
Two ways are shown above to open a file. We can use any of them. There is some difference between both syntax. In first one, inside square bracket only one parameter is required ie. file name/ file path.
Actually by default mode of opening file in read mode ie ‘r’ mode. If we do not pass any mode while opening file. Then it will open that file in read mode. In second, we have to just pass the file mode ie. ‘r’ etc.
Syntax 2:
Note: Always remember, the file name or file path you are giving should be the existing one. If file name or file path is wrong then it will return an error!
Example 1: Example of opening of text file.
#www.pythonlobby.com f = open("test.txt") if(f): print("File exist")
Output
#File exist //It must create a text file first at the same location where your python file.
Important: This program is just for opening file. Other operations we will perform later.
2.) Create File: Above we learned, how to open existing file. Here we will learn how to create own file if file is not already existing.
There are two ways to create a file:
1). Using open() method
2). Using with open() statement
(i). open(): We mainly prefer to use open() method to create a file. We will use same function ie. open() that we have used for opening file. Only difference is instead of opening file in read mode we will open file in write mode.
Syntax 1:
Here ‘w’ defines that the file we are going to open, should be open in write mode only. We open a file in write mode when we have to perform any operation on the data.
Example 1:
Here our code successfully executed and you can see that our output and file created highlighted in yellow.
Note: If file name given inside open function is already exist. Then it will override the complete data in the existing file.
2). with open() statement: This is the very handy block method. Using this method, we can perform multiple operations in a single block. Operations we can perform like write, append, update, delete etc. The advantage of using with open statement() is that you don’t need to close file after performing any updation in file. Close() method is called automatically in with open() statement.
Syntax:
Note: Here f.write() is the operation (write operation on file). We will discuss about operations on file in next articles.
Example 1:
Important: In this example we performed write operation on our file. We will discuss in detail about operation on file in next article.