-1

I am trying print a line which has string 'usb' in it. I am using python 2.7.

The following is my code:

>>> fd=open('abc.text', 'r')
>>> for line in fd.readlines():
...     print line
...
this is to test

python i/o

and other such stuff

btw what is usb

Usb is something

to look forward to..

>>> for line in fd.readlines():
...     if 'usb' in line:
...         print line
...
>>>
Right leg
  • 16,080
  • 7
  • 48
  • 81
n0unc3
  • 99
  • 2
  • 9
  • 4
    Please don't put an image of text in your question. Please put the actual text itself. Please copy-paste any examples into a code block in your question. – Robᵩ Dec 22 '16 at 04:09

1 Answers1

1

Once you loop through the lines using fd.readlines() python is at the end of the file. When you call it again there are no lines to read. To get back to the start of the file, use fd.seek(0) and then you can use fd.readlines() again.

Hasan
  • 200
  • 2
  • 12