0

Title pretty much says it all: I've spent over a day figuring out how to fetch a single line of text out of a large text file through inputting its line number, without success. So far, it seems like the only solutions people are talking about online load the entire text file into a list, but it's simply too large to do so. The structure of the .txt file I'm working with is literally just a big listing of urls, one per line.

I've tried using handle.readline(), but that didn't help the pinpointing of the specific line. Because the file is so large, I can't really justify loading all of its lines into memory using the handle.readlines() method, so that's a bust as well. I tried to write a function using a for index,line in enumerate(handle) as found online, but that weirdly returns None. Any help is appreciated.

EDIT: Some code below that does not work:

fh = open("file.txt","a+")

def offsetfunc(handle,lineNum):
    line_offset = []
    offset = 0
    for line in handle:
        line_offset.append(offset)
        offset += len(line)
    handle.seek(line_offset[lineNum-1],0)

offsetfunc(fh,1)        #Returns IndexError
print(fh.readline())    #Presumably would be viable, if the previous statement worked?

Also, using the linecache technique loads the file into memory, so yeah, that's non-viable, too.

Mew
  • 368
  • 1
  • 11
  • How `handle.readline` is not good solution? – Ilija Feb 12 '18 at 21:59
  • Possible duplicate of [How to jump to a particular line in a huge text file?](https://stackoverflow.com/questions/620367/how-to-jump-to-a-particular-line-in-a-huge-text-file) – bla Feb 12 '18 at 22:01
  • It doesn't give the possibility to target the one line I want, simple as that. (The argument for it doesn't specify a line number, but rather a byte.) – Mew Feb 12 '18 at 22:03
  • Try providing a code snippet with comments so folks can provide more cohesive answers/suggestions – Theo Feb 12 '18 at 22:15
  • There's not much code to provide, as everything given is the large .txt mentioned with the structure described, and furthermore a line number; I tried the technique @Robᵩ offered, as well as the ones in the post above, but none did the trick. – Mew Feb 12 '18 at 22:55
  • @Mew all of those should work, as well as Rob's approach. You are going to have to provide a [mcve] if it isn't working. – juanpa.arrivillaga Feb 12 '18 at 22:58

1 Answers1

1

This program might do what you want:

def fetch_one_line(filename, linenumber):
    with open(filename) as handle:
        for n, line in enumerate(handle, 1):
            if n == linenumber:
                return line
    print("OOPS! There aren't enough lines in the file")
    return None

my_line = fetch_one_line('input.txt', 5)
print(repr(my_line))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Still returns "None", though there are plenty of lines! – Mew Feb 12 '18 at 22:11
  • Does it print the "OOPS" ? – Robᵩ Feb 12 '18 at 22:15
  • Yes, it does print the OOPS. – Mew Feb 12 '18 at 22:51
  • Then there are certainly not enough lines in the file. Can you open a text editor and create a 7-line file called "input.txt" and run the above program? – Robᵩ Feb 12 '18 at 22:59
  • I did exactly so, and it still returns "OOPS" and None. – Mew Feb 12 '18 at 23:02
  • Oh, I might've found my error. Is it wrong for me to have used `handle = open(filename,"r+")` instead of `with open(filename) as handle`? I figured I didn't need the `with` syntax, since the file is already opened at the start of the program. – Mew Feb 12 '18 at 23:15