Files I/O

A file is an external storage on the hard disk from where data can be stored and retrieved. Python supports reading data from files and writing data to the files. In this part we will be dealing with operations like

Python file operation
Python file operation comprises of three basic steps. They are-

  • Open a file
  • Perform read and write
  • Close the file

Opening a file

To open file built-in function open() is used. It returns an object of File which is used with other functions.Syntax:

obj=open(file_name , access_mode , buffer)


  • file_name refers to the file which you want to open.
  • access_mode specifies the access_mode in which the file has to be open, i.e., read, write, append, etc.
  • buffer represents that buffering is performed or not, if buffer value is 0 then no buffering is performed and when buffer value is 1 then line buffering is performed while accessing the file.

Access Modes

rOpens a file for reading
rbOpens a file for reading only in binary format.
wOpens a file for writing only. Overwrites the file if the file exists.
wbOpens a file for writing only in binary format.
aOpens a file for appending. It does not overwrite the file just add the data in the file and if file is not created then it creates new file
abOpens a file for appending in binary format.

Writing into the File

write() method is used to write a string into the file.

File_object.write(string str)
i.write(“Hello Intellipaat”)

Reading from the File

read() method is used to read data from the File.


print kOutput :
Hello Intellipaat

It is used to close a file. For this purpose, close() function is used.


So, for instance, we need to the file after we are done with our file operations. We do that by following method.

print kj.close()

Methods in File Handling
There are different methods are used which are as follows:

  • rename() : This is used to rename a file.
os.rename(existing file_name, new file_name)


import os
  • remove():This method is used to delete a file.




import osos.remove(“abc.txt”)
  • chdir(): This method is used to change the current directory.


os.chdir(“new directory”)


import osos.chdir(“new directory path”)
  • mkdir() : This method is used to create a directory.


os.mkdir(“new directory”)


import osos.mkdir(“abc”)
  • rmdir() : This method is used to remove the directory.


os.rmdir(“directory name”)


import os
os.rmdir(“abc”) #where abc is a directory name which you want to remove
  • getcwd()– This method is used to show the current working directory.




import osprint os.getcwd()

Various file methods


close()To close an open file. It has no effect if the file is already closed.
flush()To flush the write buffer of the file stream.
read(n)To read at most n characters form the file. Remember that it reads till end of file if it is negative or None
readline(n=-1)To read and return one line from the file.
Remember that it reads at most n bytes, if specified.
readlines(n=-1)To read and return a list of lines from the file.
Remember that it reads at most n bytes/characters if specified.
seek(offset,from=SEEK_SET)It changes the file position to offset bytes, in reference to from (start, current, end).
tell()It returns the current file location.
writable()It returns True if the file stream can be written to.
write(s)To write string s to the file and return the number of characters written.
writelines(lines)To write a list of lines to the file.

That is how we deal with files in Python. Here, we have also discussed different file methods which will be helpful while working on real-world projects. For beginers, a lot of interview questions can be raised on this topic. Prepare yourself for the best via our Python Interview Questions.
So far, we have discussed the basics in our Python online tutorial, which are the building blocks to write a python program.
Now let us go ahead and discuss how to make our program more robust and secure from potential failures.Click on the next tutorial to find more.
Click on the next tutorial to find more or check how to get Python Certification online.

Leave a Reply

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

Solve : *
15 + 26 =