I have got a .txt file that contains a lot of lines. I would like my program to ask me what line I would like to print and then print it into the python shell. The .txt file is called packages.txt.
I have got a .txt file that contains a lot of lines. I would like my program to ask me what line I would like to print and then print it into the python shell. The .txt file is called packages.txt.
If you don't want to read in the entire file upfront, you could simply iterate until you find the line number:
with open('packages.txt') as f:for i, line in enumerate(f, 1):if i == num:break
print line
Or you could use itertools.islice()
to slice out the desired line (this is slightly hacky)
with open('packages.txt') as f:for line in itertools.islice(f, num+1, num+2):print line