Python Io
14 Aug 2020Python I/O functions
I am primarily a .NET developer who has her eyes on python for a few months now, I am really in love with python for its simplicity and its power to do almost anything I can think of. I am mostly playing with it by scraping websites and reading/writing files.
When doing file i/o there is one thing I have always found little confusing to me which are the file i/o modes. I often had to look up what each mode does and I thought I should document my learning for others and for my future reference as well.
Basic file operation that we commonly used in excel or text related operations is READ, WRITE, and APPEND.
Python I/O modes:
There are 6 modes available for the file i/o as follows.
r → Only for reading. Starting position is Zero.
r+ → For reading and writing. Position is zero.
w → Only for writing if the file exists, otherwise it creates a new file for writing at 0th position.
w+ → For reading and writing at 0th position or create if the file not exists.
a → Only for writing if the file exists, otherwise it creates a new file for writing at last position.
a+ → For reading and writing at last position or create if the file not exists.
Above all the modes are very similar with small differences.Based on our requirement we have to choose the mode. ****
So when to choose one mode over the other?
Choose r:
If you have an existing file and all you need to do is just read the content from the 0th position.
Choose r+:
****If you have an existing file, and you need to read the content from the 0th position and overwrite previous content with new.
It deletes all the content from the 0th position and write the new content what you have provided.
Choose w:
****If you want to create a new file or replace the old file with new. It checks the given file name if the file is not exists it will create a new one otherwise it delete all content from old that is over write.here also the starting position is zero.
Note: The only difference between r+ and w is w can create a new file and can’t read the content.
Choose w+:
If you want to read an existing file content from the 0th position and overwrite the content and need to create a new file if the file does not exist.
Note: It like a combination on r+ and w. Here we can create or replace the file and as well we can read and write the file from the 0th position.
Choose a:
****If you want to append the content with the previous one or you need to create a new file.
In all the above cases the position will be zero(means it deletes the previous file and write as new),but in this mode the position is the end of the file(means it append the content with the old one).
Note: It exactly similar with w, but the only difference is the position of writing.
Choose a+:
****If you want to read the file and append the content with the previous one or you need to create a new file.
Note:It exactly similar with w+, but the only difference is the position of writing.
We have some extra modes for some extra features. If you want the file in a binary modes means we can use the below modes.
wb → Its performs like w with binary mode.
wb+ → Its performs like w+ with binary mode.
ab → Its performs like a with binary mode.
ab+ → Its performs like a+ with binary mode.
Thanks for reading so far. I hope I have covered all the available modes and explained myself the best I could do, this is my first blog post and I would love to hear your thoughts and feedback.