Neither apple nor pine are in pineapple. Boxing rings are square. Writers write but fingers don’t fing. Overlook and oversee are opposites. A house can burn up as it burns down.
An alarm goes off by going on.
-Append back the entire file content using read() or readlines() and display on screen.
-Append more text of your choice in the file and display the content of file with line numbers prefixed to line.
-Display first line from 10th character onwards.
-Display last line of file.
-Read and display a line from the file. Ask user to input the line number to be read.
In [ ]:
def readfile(filename):
f = open(file,'r+')
f.write('Neither apple nor pine are in pineapple. Boxing rings are square. Writers write but fingers don’t fing. Overlook and oversee are opposites. A house can burn up as it burns down. \nAn alarm goes off by going on.')
f.seek(0)
content = f.readlines()
f.writelines(content)
for i in content:
print i
f.write('This is text of my choice')
f.seek(0)
content = f.readlines()
for i in xrange(len(content)):
print str(i) + content[i] #Print content of file with line numbers
f.seek(10)
print f.read() #Print contents from 10th character onwards
print content[-1] #Print last line
linenumber = raw_input('Enter line number: ')
print content[linenumber]
f.close()