问题
So i had 676 line program, Now i have had a issue in the past when I opened the file all my code had disapeared so this time i made a backup. However when I try to copy and paste my backedup code then run it, it gives me this syntax error:
Source code string cannot contain null bytes
This was not displayed when I first made the backup and the program was working fine. I really do not want to go through 676 lines redoing it all. Im a pretty basic programmer when it comes to Python.
I already tried removing all white space and comments/# (Red background colour is at the end of a #) Already looked through all the code and it really has no errors.
回答1:
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.
来源:https://stackoverflow.com/questions/47336788/source-code-string-cannot-contain-null-bytes