I need to get a specific line number from a file that I am passing into a python program I wrote. I know that the line I want will be line 5, so is there a way I can just grab line 5, and not have to iterate through the file?
3 Answers
If you know how many bytes you have before the line you're interested in, you could seek to that point and read out a line. Otherwise, a "line" is not a first class construct (it's just a list of characters terminated by a character you're assigning a special meaning to - a newline). To find these newlines, you have to read the file in. 
Practically speaking, you could use the readline method to read off 5 lines and then read your line. 
Why are you trying to do this?
3 Comments
you can to use linecache
import linecache
get = linecache.getline
print(get(path_of_file, number_of_line))
2 Comments
I think following should do :
line_number=4
# Avoid reading the whole file
f = open('path/to/my/file','r')
count=1
for i in f.readline():
    if count==line_number:
        print i
        break
    count+=1
# By reading the whole file
f = open('path/to/my/file','r')
lines = f.read().splitlines()
print lines[line_number-1]     # Index starts from 0
This should give you the 4th line in the file.
4 Comments
readline (also, you can say for i in f). In the second case, you're reading the entire file into memory and then "parsing" it for newlines which is probably not a good idea.

\nwithin the file. Is this what you are asking? Or do you mean, that you don't want to load the entire file in order to retrieve line 5?