MyText.txt
This is line 1
This is line 2
Time Taken for writing this# 0 days 0 hrs 1 min 5 sec
Nothing Important
Sample Text
Objective
To read the text file and find if "Sample Test is present in the file. If present print the time taken to write the file (which is a value already inside the file)"
My Code
with open('MyText.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split())
f.close()
The code is working fine. I want to know if this code can be made even better. Considering I am new to Python, I am sure there would be a better way to code this. Can anyone suggest alternative/faster approaches for this?