the error is too long so I haven't put it in the title. Here is the error:
Traceback (most recent call last):
File "./auto_change_shebang_and_permission.py", line 12, in <module>
shebang = f.readline()
File "/usr/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 3: invalid start byte
However I've never seen this error before. I written a program to auto change the SheBang on Linux and it use os.walk
to get the files path and open them, here is the program:
for root, dirs, files in os.walk(sys.argv[1]):
for name in files:
# to except the hidden file
if root[:3] == './.':
pass
elif name[0] == '.':
pass
# open the file, read the SheBang, and change it.
else:
with open(os.path.join(root, name), 'r') as f:
shebang = f.readline()
if shebang[:2] != '#!':
with open(os.path.join(root, name), 'w') as e:
e.write('#!/usr/bin/env python3\n'+shebang+f.read())
else:
if shebang != '#!/usr/bin/env python3':
with open(os.path.join(root, name), 'w') as e:
e.write('#!/usr/bin/env python3\n'+f.read())
os.chmod(os.path.join(root, name), 493)
It's working, but it will raise that error. I don't know where is wrong.
Okay, now I understand what's wrong. Because if root[:3] == './.'
only works when the sys.argv[1]
is .
.
Then, like @AnandSKumar said. The program got a file that is not a text file.
So now I edited my code like this:
if fnmatch(root, sys.argv[1]+'/.*'):
pass
And now it's working so good, thanks @AnandSKumar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.