0

So I have a folder with two .txt Files A.txt and B.txt.

And I have a program that looks like this:

def main():

    f = open("/Users/MyName/Desktop/MyPythonFolder/ProjectFolder/A.txt","r+")

    print(f.read())
    
main()

When I run this program, IDLE crashes... but when I reboot IDLE and try to use B.txt it works perfectly fine. In addition, if I try to do print(len(f.read())) or anything manipulating the file such as write, truncate, or even getting data from the file such as len(), split(), etc it works fine. I've identified this is because A.txt is filled with \x00 at the beginning. Does anyone know why/how these became present?

Henrique Lee
  • 59
  • 1
  • 1
  • 7

1 Answers1

1

To fix this, just decode the data:

print line.decode('utf-16-le').split()

Or do the same thing at the file level with the io or codecs module:

file = io.open('data.txt','r', encoding='utf-16-le')

got it from another question here

safiqul islam
  • 546
  • 3
  • 13