Python: Source Code String Cannot Contain Null Bytes

Python: source code string cannot contain null bytes

For posterity: I had the same problem and fixed it using,

sed -i 's/\x0//g' FILENAME

The file seemed to be messed up in numerous ways (wrong endings, etc); no idea how...

See https://stackoverflow.com/a/2399817/230468

Source code string cannot contain null bytes

The problem is likely just what the error message tells you: your back-up copy somehow got "infected" with one or more null bytes (ASCII value 00). Paste your code one block at a time -- say, 50 lines -- to find which contain illegal bytes. Delete the most recently-added code, maybe 5 lines at a time, to find which has the null byte. Retype the offending line, and go on to the next.

Another possibility is to write a simple Python script that reads the file and removes the null bytes use the string replace method:

with open("homework.py", 'r') as infile:
hw = infile.readlines().replace(chr(0), '')

Now close the file, open it again for 'w', and dump the hw variable to it.



Related Topics



Leave a reply



Submit