0

So I have a very large file with a few thousand line that look something like this:

this a line
some line are long like this one
some are very longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg like this
some are even longer than that
some are small like the line after this one
a
and this continue on for a few more thousand line

So for example I want to edit line 4724 just a random line from it current value: this is a line that im using as an example to I want to edit the line into this. How can I do that in python. If you need more info just ask me in the comment.

Edit: the file is something around 300KB and yes I have try readlines() and it only return 1/3 of the whole file. So that why i say memory is a problem

Max Nguyen
  • 179
  • 1
  • 8
  • how large is the file? If it's anything reasonable (such that you can load it into memory) you could just naively read the whole file, split on lines, update line 4724, and write it back. – Adam Smith Dec 28 '21 at 19:30
  • Does this answer your question? [How to read a large file - line by line?](https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line) – itprorh66 Dec 28 '21 at 19:31
  • @AdamSmith it about 100KB or more that why i can't so readlines – Max Nguyen Dec 28 '21 at 19:31
  • @MaxNguyen 100KB is nothing on any hardware made in the past (human) generation or so. Just read it. – Adam Smith Dec 28 '21 at 19:33
  • 1
    Have you *tried* `readlines()`? 100k is nothing. – MattDMo Dec 28 '21 at 19:33
  • It 222kb in this case that I'm talking about with 4671 lines and do `readlines()` return only 1/3 of the file – Max Nguyen Dec 28 '21 at 19:36
  • 1
    @MaxNguyen readlines will never return only part of a large file. Something else is wrong if that's your result. Maybe you have mixed line-endings in the file that appear to be correct in whatever editor you're using to view the file, but means that what you think a "line" is does not match the actual reality of what a line is? – Adam Smith Dec 28 '21 at 19:37
  • @MaxNguyen if your file was too big, it would crash and throw a stacktrace ending in `MemoryError`. – Adam Smith Dec 28 '21 at 19:39
  • Because the file is too big, so `realines()` encounter a memory issue and only return part of it. – Max Nguyen Dec 28 '21 at 19:40
  • @mkrieger1 the accepted answer on that question is from 2008 and that's Python2 code. – Adam Smith Dec 28 '21 at 19:43
  • 1
    @AdamSmith then write a new answer or update the existing ones. It's still the same question. (The accepted answer has been maintained until 2020 btw) – mkrieger1 Dec 28 '21 at 19:47
  • So the answer with the most vote actually solve my problem thanks @mkrieger1 – Max Nguyen Dec 28 '21 at 19:48

1 Answers1

1

Based on the size of the file, given as 100KB in the comments, you should just naively read the file and change the line in question.

LINE_TO_CHANGE=4724

with open('my_file.txt') as f:
    lines = f.readlines()

lines[LINE_TO_CHANGE - 1] = 'edited line\n'

with open('my_file.txt', 'w') as f:
    f.writelines(lines)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112