Python File Handling

Python also supports the file handling and allows users to files, which is addition, read and  Write files, along with a lot of things to download files, to work on files.The idea of ​​the case study was raised in other languages, but the implementation is complicated or deleted, but, like Python’s other ideas, this idea is simple and short. Each part of the code includes a series of characters that shaped the text file.
We use open source () in Python to open a read or write file. As described previously, open (), restores a file item. To restore a file, we use open (), with two modules, which accept the file name and format, must be read or duplicated. Then, the text is: open (the file name, format). There are four types of Python formats and how to open the file:

  1.  “r”  read
  2.  “w”  write
  3.  “a”  append
  4.  “r+”  both reading and writing

Opening a File:

Before you read / write, you must first open the file. The word to open the file is.

f = open(filename, mode)

The open Function () accepts two arguments in the file name and format. The filename is a debate which defines the file name and path, and the method is also a discussion that uses to describe how to use the file, for example, reading or writing. And f is the object of a file, called the ‘file folder’.

Enroll Now: Python Training in Chennai

Closing a File:

After completing reading the file, it is necessary to close the file using the method () in the same way,

f.close()  # where f is a file pointer

Enroll Now: Python Training in Bangalore

Different modes of opening a file are

“r”  Open a file for read only
“w”  Open a file for writing. If file already exists its data will be cleared before opening. Otherwise new file will be created
“a”  Opens a file in append mode i.e to write a data to the end of the file
“wb”  Open a file to write in binary mode
“rb”  Open a file to read in binary mode

Let’s now look at some examples.

Writing Data to the File:

>>> f = open('myfile.txt', 'w')    # open file for writing
>>> f.write('this first line\n')   # write a line to the file
>>> f.write('this second line\n')  # write one more line to the file
>>> f.close()                      # close the file

Reading data from the file

To read data back from the file you need one of these three methods.

read([number]) Return specified number of characters from the file. if omitted it will read the entire contents of the file.
readline() Return the next line of the file.
readlines() Read all the lines as a list of strings in the file

Reading all the data at once.

>> f = open('myfile.txt', 'r')
>>> # read entire content of file at once
"this first line\nthis second line\n"
>>> f.close()

Reading all lines as an array.

>>> f = open('myfile.txt', 'r')
>>> f.readlines() # read entire content of file at once
["this first line\n", "this second line\n"]
>> f.close()

Reading only one line.

>>> f = open('myfile.txt', 'r')
>>> f.readline() # read the first line
"this first line\n"
>>> f.close()

Appending data

To append the data you need to open the file in ‘a’  mode.

>>> f = open('myfile.txt', 'a')
>>> f.write("this is third line\n")
>> f.close()

To getting expect level training for Python training in your Location – Python Training in Chennai | Python Training in Bangalore | Python Training in Pune | Python Training institute in Chennai Python Interview Questions | Python Course in Chennai

Leave a Reply

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