Python Does Not Read Entire Text File

前端 未结 3 539
梦毁少年i
梦毁少年i 2020-12-31 07:05

I\'m running into a problem that I haven\'t seen anyone on StackOverflow encounter or even google for that matter.

My main goal is to be able to replace occurences o

相关标签:
3条回答
  • 2020-12-31 07:35

    Try:

    f = open("filename.txt", "rb")
    

    On Windows, rb means open file in binary mode. According to the docs, text mode vs. binary mode only has an impact on end-of-line characters. But (if I remember correctly) I believe opening files in text mode on Windows also does something with EOF (hex 1A).

    You can also specify the mode when using fileinput:

    fileinput.input("filename.txt", inplace=1, mode="rb")
    
    0 讨论(0)
  • 2020-12-31 07:39

    If you use the file like this:

    with open("filename.txt") as f:
        for line in f:
            newfile.write(line.replace("string1", "string2"))
    

    It should only read into memory one line at a time, unless you keep a reference to that line in memory.
    After each line is read it will be up to pythons garbage collector to get rid of it. Give this a try and see if it works for you :)

    0 讨论(0)
  • 2020-12-31 07:48

    Are you sure the problem is with reading and not with writing out? Do you close the file that is written to, either explicitly newfile.close() or using the with construct?

    Not closing the output file is often the source of such problems when buffering is going on somewhere. If that's the case in your setting too, closing should fix your initial solutions.

    0 讨论(0)
提交回复
热议问题