0

I'm trying to make a quick script that takes the text from a file, encodes the text to hex and put that in another file.

path = "C:\\..."
finalname = "example.txt"

    with open(path,"r") as fr:
        content = fr.read().encode("hex")
        fr.close()

    with open(finalname,"wb") as fw:
        fw.write(content)
        fw.close()

But when I was testing it I see that when the scripts works with a "big" file like 10MB or more the scripts only take a piece of the whole text, and if that piece was at the beginning of the text I would understand that they would be limitations of the .read() function, but the piece that it picks up is in the middle of the text without any reason.

Nick
  • 4,820
  • 18
  • 31
  • 47
  • Please check this: https://stackoverflow.com/a/7134355/1578952 – thirdDeveloper Apr 18 '20 at 22:56
  • 7
    FYI: Don't include `fr.close()` within the `with` statement's code block. The point of using the context manager is that the file will be closed automatically when the code block is exited. – Warren Weckesser Apr 18 '20 at 22:56
  • 1
    You could gather more info by first doing `print(os.stat(path).st_size)` and then breaking your read into a couple of steps: `buf = fr.read();print(lien(buf));content=buf.encode("hex")`. Those two numbers should be the same unless the file itself has multibyte characters such as utf-8. – tdelaney Apr 18 '20 at 23:14
  • 1
    What version of python are you using... 2.7? – tdelaney Apr 18 '20 at 23:19
  • 2
    Please provide a [mcve]. – AMC Apr 18 '20 at 23:19
  • 1
    I tried this on a 170M file with python 2.7 on linux and it worked. `file.read()` with no size given is defined to read the entire file. Its a bug if it doesn't. There can be issues with files > 2GB (eithe because of python or older operating system support of large files), but you are nowhere near that. Are you on a reasonably new windows and python version? – tdelaney Apr 18 '20 at 23:24
  • Yes is python 2.7 – Juaan10 Apr 19 '20 at 21:14
  • And is the newer version of python 2.7 and Windows is updated – Juaan10 Apr 19 '20 at 21:16

0 Answers0