profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, November 7, 2017

Python 3.6 Read and write files


One of the things that you do as a programmer and especially as a data analyst is to read from various files and write to many files. Data is in the form of files and not always stored in a database. Many a times you have to do a lot of string or data manipulation to extract the information that you need or want.
Let us see first how we can create a file. I am gonna be using pyzo editor but you use whichever you are comfortable with.
Create

open('ambig1', 'w')

the above lines mean that open a file named ambig1 in the w (write) mode and if the file is not present then create it and open it in w mode.
So what are these modes?

  • 'r' : use for reading
  • 'w' : use for writing
  • 'x' : use for creating and writing to a new file
  • 'a' : use for appending to a file
  • 'r+' : use for reading and writing to the same file
I do not wish to cover all the modes just to make sure you have something to explore, otherwise anything which is spoon fed and has no element of curiosity or struggle, that thing gets boring.

The above code creates the file in the system's default directory but you can always mention a path.
I want to take this opportunity to get a bit tricky. Please create a new temp.py file in any directory of your choice and do the following

import os
cwd = os.getcwd() # Current Working Directory
open(cwd+'/somefile.txt', 'w') # old method
open('{}/somefile1.txt'.format(cwd), 'w') # new method

You will notice that 2 files gets created. The last method to create a file is the recommended industry best practice if you are developing in a group environment but if you are just scripting then you can use whichever is fast and convenient to you.
Let us write something to it shall we!.

import os
cwd = os.getcwd()
f = open('{}/bee.txt'.format(cwd), 'w')
f.write('my name is mogambo')
f.write('my name is mogambo')
f.write('my name is mogambo')

If you now go and open the new file bee.txt you will notice that the line 'my name is mogambo' is written only once? why?. Because everytime you are writing to the file and not appending. In short it is recreating and rewriting every time. Be careful!. If you do this to a file with already some data in it then you are in trouble. All the stuff is gone in that file.

import os
cwd = os.getcwd()
f = open('{}/bee.txt'.format(cwd), 'w') # create and open in write mode
x = open('{}/bee.txt'.format(cwd), 'a') # open file in append mode
x.write('my name is mogambo') # open file in append mode and append 'my name is mogambo'
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'

If you open the bee.txt file you will see something like this
my name is mogambomy name is mogambomy name is mogambo
not quite what we wanted. All this should have been in the next line. let us change the code a bit

import os
cwd = os.getcwd()
f = open('{}/bee.txt'.format(cwd), 'w') # create and open in write mode
x = open('{}/bee.txt'.format(cwd), 'a') # open file in append mode
x.write('my name is mogambo'+' ') # open file in append mode and append 'my name is mogambo' and adds a space
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'
x.write('\n') # add a new line
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'

So here we have it. Please observe the comments next to the code. the resulting data in the file will now be as below
my name is mogambo my name is mogambo
my name is mogambo
What if we want to write and then read the file too? because it is inefficient to open the file everytime we write to it, rather we look at it whenever we want from the python console itself. So our code now will be something like this

import os
cwd = os.getcwd()
f = open('{}/bee.txt'.format(cwd), 'w') # create and open in write mode
x = open('{}/bee.txt'.format(cwd), 'a') # open file in append mode
x.write('my name is mogambo'+' ') # open file in append mode and append 'my name is mogambo' and adds a space
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'
x.write('\n') # add a new line
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'

y = open('{}/bee.txt'.format(cwd), 'r') # open file in read more
print(y.read()) # print the contents of the file

This is all about reading, writing, appending to a text file. You however see that we have opened the file in 3 modes and it is a best practice to close it once you are done. So our final code will be...

import os
cwd = os.getcwd()
f = open('{}/bee.txt'.format(cwd), 'w') # create and open in write mode
x = open('{}/bee.txt'.format(cwd), 'a') # open file in append mode
x.write('my name is mogambo'+' ') # open file in append mode and append 'my name is mogambo' and adds a space
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'
x.write('\n') # add a new line
x.write('my name is mogambo')  # open file in append mode and append 'my name is mogambo'

y = open('{}/bee.txt'.format(cwd), 'r') # open file in read more
print(y.read()) # print the contents of the file

f.close()
x.close()
y.close()
of course you have to take out those line numbers. They are just for your reference.