Python Read and Write File

In this tutorial, you’ll learn how to read and write file in Python.

Program to Write to File

Output:

this program will create a new file (if not exist) named as “newtext.txt” in the same directory where our program is located and write a line “something to be write”.

Program to Read File

Output:

something to be write

As we have seen the program, it is very easy to read and write into a file in python. In python we can work with two type of files Text files and Binary files. In this tutorial we are working with text file.

Python Read and Write File

Let’s understand above programs.

open() is just a function to get the file object. It gets the file object and returns it to the f, we can name f as we want. using file object’s methods and attributes we can access the information that file have.

open() takes two arguments, first the file name (in our case, its “newtext.txt”) and second one is for access mode (optional).

Access Modes: it is an attribute of file object. Which tells, in which mode the file is opened.

There are 4 types of access modes.

  1. r: r stands for reading mode. If we open our file in this mode then we can only read the file’s information.
  2. w: w stands for writing mode. If we open our file in this mode then we can only write into the file, can’t read. If the file is not already exist then it will create new one, if it exists then it will delete all the previous content from the file and start writing from starting of the file.
  3. a: a stands for appending mode. It is same as writing mode but the difference is it will not delete the previous content of the file. It starts writing from the last of the file. In other words, it will append (concatenate) the new content with previous one.
  4. r+: special read and write mode. If we open the file using this mode then we can both read and write into the file.

Note: access mode is optional in open() method. By default the file will open into read mode (r).

Come to the next line of our program.

write() is a method of file object to write into the file. We will write in the parenthesis, what we want to write into the file (as shown above).

To read from the file:

read() is a method of file object to read the information stored in a file object. read() method read all the information from the given file object and returns it.

Last line in our program:

close() is a method of file object to close the opened file. Which means we can’t access the file in our program now.

There are many other useful methods of the file objects like:-

read(int): if we pass any digit to the read() method then it will return same number of characters from the file object.

Example:

Let’s say we have a text file named as “newtext.txt” and “the crazy programmer” have written into it.

Output:

the crazy

readline(): it is used to read a single line (separated by enter key or \n ) from the file object.

Example:

Lets say our text file is look like this:

line number 1
line number 2
line number 3

Output:

line number 1

Output:

line number 1
line number 2

readlines(): it reads all lines in the file object and return it as a List of lines.

Example:

Output:

[ ‘line number 1\n’ , ‘line number 2\n’ , ‘line number 3\n’]

If we want to print a specific line then:

Output:

line number 3

seek(int): it sets the position of reading and writing pointer. Using seek() method we can navigate to start or last of the file object.

This method takes 0,1,2 as argument.

  • 0 – takes pointer to the starting
  • 1 – current position of the pointer
  • 2 – takes pointer to the end

Example:

Output:

line number 1
line number 2
line number 1

We can also use with statement to open the files for better indentation.

It will create a new file named as “file.txt” and write “example line” into it.

Comment below if you have queries regarding above python file handling tutorial.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.