Learn Python Programming

Python Files Input/Output

When it comes to files, there are a couple of typical things that you will need to do. You need to open and close a file, even if it’s a Notepad file, or a Python file that you must just created after the first one. The thing that you do with any file is that you open it or you close it.

The next thing are you need to write a file or to read from a file. And then some sort of maintenance operations such as renaming the files, moving the files or deleting the files etc.

So before going ahead, you need to understand first opening and closing a file how that sort of work in Python.

The thing is that opening and closing a file works with modes.

It’s not a simple as opening a physical file in the sense that there are different modes in which you can open up a Python file.

There are several modes to work with file in Python:

  • Read only mode
  • write mode
  • write+ mode (it creates the file, if it does not exist)

And all these are for the sake of protection in the sense that if a file was always open in write only mode, it it was that you can write and read to it all the time. You might end up doing certain things to a file, certain operations, you might end up messing up the data inside the file.

It is basically a best practice that you open the file with the correct kind of mode based on what you are going to use it for. If you are not going to write to it, please don’t open it in a write mode, open it in a read mode. Therefore, there is a closing a file. Suppose you are opening a huge file of size 500 mb. It contains 10 million addresses or names. If you do not close the file, what happens is that when you open a file it gets loaded into the RAM. If you do not close the file properly, you do not do that management bit of the file properly, then it will remains in the memory and that can potentially cause a memory leak.

Open Function

You can open a file using Python’s built-in open() function.

file_object = open(file_name, [access_mode])

Here are the parameter detail:

  • file_name : The file_name argument is a string value that contains the name of the file that you want to access.
  • access_mode: The access_mode determines the mode in which the files has to be opened, i.e., read, write, append.

Open Function – Access Modes

rThis is the default mode. Opens a file for reading only.
rbOpens a file for reading only in binary format.
r+Opens a file for both reading and writing.
rb+Opens a file for both reading and writing in binary format.
wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
aOpens a file for appending.
abOpens a file for appending in binary format.
a+Opens a file for both appending and reading.
ab+Opens a file for both appending and reading in binary format.
w+Opens a file for both writing and reading.
wb+Opens a file for both writing and reading in binary format.

Writing Files

The write() method does not add a new line character ‘\n’ to the end of the string.

The write() method writes content to an open file Python strings can have binary data and not just text.


Reading Files

The read() method reads a string from an open file.

It is important to note that python strings can have binary data apart from the text data.


It you provide a count, that is the number of characters that it will read at a time. It you do not pass a parameter count count, it will read everything.

Renaming Files

The rename() method takes two arguments, the current filename and the new filename. rename() is the method from os module. It is really usually done with the help of the operating system.

os.rename(current_file_name, new_file_name)

Deleting Files

You can use the remove() method to delete the files by supplying the name of the file to be deleted as an argument. remove() is the method from os module.



The softspace() method returns a Boolean, whether a space character needs to be printed before another value when using print statements.


File.seek() and File.tell()

The seek(offset[, from]) method changes the current file position.


The tell() method tells you the current position within the file. In other words, the next read or write will occur at that many bytes from the beginning of the file.