Наиболее гибкий вариант - переключиться на базу данных или другую более сложную файловую структуру на диске.
Однако, возможно, есть веская причина, по которой вы предпочитаете хранить текст в виде простого текстового файла....
Поскольку у вас есть контроль над тем, как создаются файлы, одним из вариантов является просто записать второй файл, который содержит только начальные позиции (в байтах) каждой строки в другом файле.
Это потребует немного больше работы, но вы можете сделать что-то вроде этого:
class IndexedText(object):
def __init__(self, filename, mode='r'):
if mode not in ['r', 'w', 'a']:
raise ValueError('Only read, write, and append is supported')
self._mainfile = open(filename, mode)
self._idxfile = open(filename+'idx', mode)
if mode != 'w':
self.indicies = [int(line.strip()) for line in self._idxfile]
else:
self.indicies = []
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self._mainfile.close()
self._idxfile.close()
def __getitem__(self, idx):
position = self.indicies[idx]
self._mainfile.seek(position)
# You might want to remove the automatic stripping...
return self._mainfile.readline().rstrip('\n')
def write(self, line):
if not line.endswith('\n'):
line += '\n'
position = self._mainfile.tell()
self.indicies.append(position)
self._idxfile.write(str(position)+'\n')
self._mainfile.write(line)
def writelines(self, lines):
for line in lines:
self.write(line)
def main():
with IndexedText('test.txt', 'w') as outfile:
outfile.write('Yep')
outfile.write('This is a somewhat longer string!')
outfile.write('But we should be able to index this file easily')
outfile.write('Without needing to read the entire thing in first')
with IndexedText('test.txt', 'r') as infile:
print infile[2]
print infile[0]
print infile[3]
if __name__ == '__main__':
main()