I don’t know why, but when I try to read a file with the file.read(), Python doesn’t recognizes the first line of the file.. It’s an interpreter bug, or it’s my fault?
You have a copy of the program here (showing the read result): http://pastebin.ubuntu.com/1032832/
This is the code which causes the problem:
if wfile.readline() != "#! /usr/bin/env python\n":
before = wfile.read()
wfile.seek(0)
wfile.write('#! /usr/bin/env python\n' + before)
wfile.close()
os.chmod(file, 777)
The Python version which I use for tests is Python 2.5.1 for iOS (Cydia port). My device is an iPad 2.
You are reading the first line of the file with the
readline()function in yourif-statement. By the time you get toread(), the first line has already been read.The subsequent
write()will write whatwfile.read()has read.It looks like you want to check if the first line of the file contains the appropriate
#!/usr/bin/...line. If not, you want to insert it ahead of the current first line, and then write the original first line below it. This will do it:This way you save the original first line in the variable
beforefor later use.Note: using
withwill automatically close the file for you when you are done, or an exception is encountered.