Learn Python Fundamental in 30 Days — Day 10(File Handling)

devops everyday challenge
devops-challenge
Published in
3 min readMay 10, 2017

To open a file in Python we use built-in open() function which accept number of arguments

open(file,mode,encoding)
  • file: path to file(required)
  • mode: read/write/append,binary/text
  • encoding: text encoding to use(it depend upon system to system)
>>> import sys>>> sys.getdefaultencoding()‘utf-8’

Python File Mode

r for readingw for writingr+ opens for reading and writing (cannot truncate a file)w+ for writing and reading (can truncate a file)rb+ reading or writing a binary filewb+ writing a binary filea+ opens for appending

Writing to first file

>>> f = open(“testfile”,”w”)>>> f.write(“This is our first test file”)27>>> f.close()

Output

ls -l testfile-rw-rw-r — 1 plakhera wheel 27 Apr 20 17:17 testfilecat testfileThis is our first test file

To read a file

>>> a = open(‘testfile’,’r’)>>> a.read()‘This is our first test file’# To read the first 5 characters
>>>
a.read(5)
'This '# Now if we try to read the rest of file
>>>
a.read()
'is our first test file'

Now if the file has multiple line and we want to read line by line

>>> x.readline()‘My new file \n’>>> x.readline()‘Ok one more line’

Now if we want to append to an existing file use append(a) mode

>>> f = open(“myfile”,”a”)>>> f.write(“Testing append mode”)19>>> f.close()>>>>>>>>>>>> g = open(“myfile”,”r”)>>> g.read()‘My new file \nOk one more lineTesting append mode’

Now lets take a look at one more example

import sys
def readfile(filename):
f = open(filename,'rt')
for
i in f:
print(i)
f.close()
if __name__ == '__main__':
readfile(sys.argv[1])

Output

python3 exception.py testfilethis is a test fileOne more line to read

As you can see we have a problem here, each line is already terminated by anewline and then print adds it own

To fix that we can strip the newline

import sys
def readfile(filename):
f = open(filename,'rt')
for
i in f:
i = i.strip() <---
print(i)
f.close()
if __name__ == '__main__':
readfile(sys.argv[1])

OR we can use the write method of standard out stream

import sys
def readfile(filename):
f = open(filename,'rt')
for
i in f:
sys.stdout.write(i) <--
f.close()
if __name__ == '__main__':
readfile(sys.argv[1])

As you see in all the above examples we are using close to close the file. close is important as we are telling the underlying the OS that we are done with it,if we are not closing the file then there is a possibility that we might loose the data.There may be pending writes that buffered up which might not get written completely. Also if we are opening lots of files we might run out of system resources.

So might need some way that we always remember to close a file. Python implements a resource cleanup called with-block.

So now our code will look like this

import sys
def readfile(filename):
with open(filename,'rt') as f: <--
for i in f:
sys.stdout.write(i)
if __name__ == '__main__':
readfile(sys.argv[1])

We don’t need to call close explicitly, as with will take care of it whenever execution exits the block.

So this end of Day10, In case if you are facing any issue, this is the link to Python Slack channel https://devops-myworld.slack.com Please send me your details * First name * Last name * Email address to devops.everyday.challenge@gmail.com, so that I will add you to this slack channel

--

--