how to open a file for both reading and writing in python

File handling is the integral part in most of the programming languages. Python provides built-in file handling methods to open, close, read and write files to read and store data. In this tutorial, we will learn how to open a file for both reading and writing in python.

We must first open a file before we can read from or write to it. Then, close the file once we are finished so that the resources associated with the file can be released. Thus, there are three steps in any file handling operation which include:

  • Open a file
  • Perform the desired operation such as read or write text
  • Close a file

File access modes defines that how the file is used once it is opened. There are six different accessing modes which are used to either read or write or perform read and write operations simultaneously.

Before going towards the actual coding examples, keep in mind that if you are working on the text file, python takes the EOL (end of line) or in programming language “\n” as the terminating point for the line in the text file and store that in the file handler.

In this tutorial, we will discuss about file reading and writing. The syntax to open a file is:

File_object = open("File_name","Access_mode")

Note that file to be read or write should be in the same directory in which your python file is present. If not, then provide the complete path of that file. Otherwise, the code will give you the file error.

If you want to learn more about Python Programming, visit Python Tutorials.

We will use two different methods to both read and write in a file.

  • Both read and write file by using Access mode “r+”
  • Both read and write file by using Access mode “w+”

both Reading and writing file by using Access mode “r+”

Python provides access modes which allows us to handle and decide what should be done with the files once they are opened. The read and write access mode (‘r+’) is used for both reading and writing to files. The code below open the file “myfile.txt” in read and write mode and read all the lines in a text file.

file = open("myfile.txt", "r+") 
file.readline()  

In above code, we have used a command file.readline() to read the specific line of the file. We can also specify the number of line by passing an argument ‘n’ inside the file.readline(n) command.

file = open("myfile.txt", "r+") 
file.read()

Here, read() is used to just get the specific bytes in form of string of the given file. we can also write it as read(n).

To write into the file, we can use write() the command as shown below. The text to be written is provided in the form of an argument inside the write() command.

file.write("any data")

We can also use writelines() function to write all the data once.

myline = "Welcome to entechin.com\n,"Hope we learn something from here\n"
file.writelines(myline)

Both Reading and writing file by using Access mode “w+”

Another access mode which is used for both reading and writing is ‘w+’ mode. It is almost similar to ‘r+’ access mode.

file = open("myfile.txt", "w+") 

In the above code, w+ is to for reading and writing. If the file exists, then the previously stored data will be overwritten and previous data will be deleted. This is the only difference between ‘r+’ and ‘w+’ modes. Once the file is opened using ‘w+’ mode, you can use read, readlines, write() and writelines() method to read and write data.

Leave a Comment

Your email address will not be published.