100 Days of DevOps — Day 89-Python Files I/O

Prashant Lakhera
3 min readMay 10, 2019

--

To view the updated DevOps course(101DaysofDevOps)

Course Registration link: https://www.101daysofdevops.com/register/

Course Link: https://www.101daysofdevops.com/courses/101-days-of-devops/

YouTube link: https://www.youtube.com/user/laprashant/videos

Welcome to Day 89 of 100 Days of DevOps, Focus for today is Python Files I/O

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

open(file,mode,encoding)
  • file: the 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 the 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 lines 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 let's 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 a newline 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 lose 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.

Looking forward from you guys to join this journey and spend a minimum an hour every day for the next 100 days on DevOps work and post your progress using any of the below medium.

Reference

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet