Python File Open Function

python-file-open-fucntion

In many programming, for file handling, there is a special function named Python File Open Function. With the help of this function, we can create files, read files, write and append on the files. In all file handling activities, we use Python File Open function.

 

Python File Open function has two different parameters. These parameters are given below:

 

  • filename
  • mode

 

The first parameter, filename, shows the file that we will create, read or modify. It is the file that we will do something about it.

 

The second parameter is the mode that we will use. There are different modes and according to these modes, our file handling action is determined. These modes are given below:

 

  • r”    To read the file. Returns error if there is no such a file.
  • a”    To open the file to append to the end of the file. If there is no file, it creates.
  • w”   To open the file to write and overwrite. If there is no file, it creates.
  • x”    To create the file. Returns error if there is such a file.

 

pyhthon-open-function-file-operation-modes

 

When we use “r” as a second parameter of Python File Open function, this means that we will read the file. So, the file must be an existing file to read it. If it is not existing, then we will receive an error.

 

If we use “a” or “w” as a second parameter, this means that, we will modify the content. With “a”, we can append new characters, sentences etc. at the end of the file. With “w”, we can overwrite the context, in other words we can change the complete content.

 

Lastly, if we use “x” mode, this means that we are creating a new file. So, there mustn’t be such a file. If we try to create a file with a filename that is already existing, we will receive and error.

 

Beside these actions, we can also delete existing files and folders with different python file remove methods.

 

So, how can we use Python File Open function? Let’s give an example for this. Think about that, there is a file named filexyz.txt in the same folder with Python. We will use this function only to read it. For this, we will use the below code:

 

x = open("filexyz.txt", "r")

 

Here, the first parameter is the file name and the second parameter is the mode that we will use as action. Here, we will use “r” mode.

 

 

We will handle each of the file handling actions in special lessons. But here, let’s give a brief info for each of these activities.

 


 

Python File Read

 

To read an existing file, we use Python File Open function with filename and “r” parameter.

 

Think about that we have a file named filexyz.txt. To read the content of this file, we will use the below code:

 

x = open("filexyz.txt", "r")
print(x.read())

 

In this example, we have also used read() method of python file object. The return of this Python File Read code will be the content of filzexyz.txt.

 

Hello, How are you?
Life is Perfect!

 

python-file-read-ipcisco.com-1


 

Python File Write

 

To write on an existing file, or to modify it, we use Python File Open function with filename and “w” or “a” parameters. “w” parameter is the completely modify parameter. On the other hand, “a” is the append parameter that adds anything at the end of the content.

 

Think about that we have a file named fileabc.txt. Its content is like below:

 

Weak people revenge.
Strong people forgive.
Intelligent people ignore.

 

To see the content of this file, we will use the below read code firstly.

 

 

x = open("fileabc.txt", "r")
print(x.read())
x.close()

 

The output will be:

 

Weak people revenge.
Strong people forgive.
Intelligent people ignore.

 

Now, we will change the the content of this file with write (w) mode. To do this, we will use the below code:

 

x = open("fileabc.txt", "w")
x.write("Simplicity is the Ultimate Sophistication.")
x.close()

x = open("fileabc.txt", "r")
print(x.read())

 

The output of this Python File Write code and the content of the demo file will be like below:

 

Simplicity is the Ultimate Sophistication.

 

python-file-write-ipcisco.com-1

 

Now, let’s use append mode to add a new sentence to this existing content.

 

x = open("fileabc.txt", "a")
x.write("A Perfect Quote!")
x.close()

x = open("fileabc.txt", "r")
print(x.read())

 

With this Python File Write code, the new content of this demo file will be like below:

 

Simplicity is the Ultimate Sophistication.A Perfect Quote!

 

python-file-append-ipcisco.com-1

 


 

Python File Create

 

To create a file, we use Python File Open function with filename and “x” parameter.

 

Think about that we will create a file named Ourfile.txt. To crate this file, we will use the below code:

 

a = open("Ourfile.txt", "x")

 

how-to-create-files-with-python-ipcisco-2

 

After this code, we have a file Ourfile.txt.

 


 

Python File Remove

 

To remove an existing file, we need to import a module named os module. After that we will use remove method in this module to delete an existing file.

 

Below, we will delete existing myfile.txt with the help of remove method od os module.

 

import os
os.remove(“myfile.txt”)

 

python-file-delete-how-to-dekete-files-python

 


 

File Handling With Python Open Function

 

Python File Open Function is the key function for file handling in python. For various reasons, you will work with files during your coding activities. And this function will be the most used function with files.

 

To work with files on python, you can increase your experience with different python file handling examples. With more examples, you will be more familiar with these methods.

 

We have seen the summary of these methods here. If you would like to learn more on these methods, you can follow the other python file handling lessons. We will give more and detailed examples in each of these lessons.

Back to: Python Programming Course > Python File Handling

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k