1#!/usr/bin/env python3 2 3# Fix Python script(s) to reference the interpreter via /usr/bin/env python. 4# Warning: this overwrites the file without making a backup. 5 6import sys 7import re 8 9 10def main(): 11 for filename in sys.argv[1:]: 12 try: 13 f = open(filename, 'r') 14 except IOError as msg: 15 print(filename, ': can\'t open :', msg) 16 continue 17 with f: 18 line = f.readline() 19 if not re.match('^#! */usr/local/bin/python', line): 20 print(filename, ': not a /usr/local/bin/python script') 21 continue 22 rest = f.read() 23 line = re.sub('/usr/local/bin/python', 24 '/usr/bin/env python', line) 25 print(filename, ':', repr(line)) 26 with open(filename, "w") as f: 27 f.write(line) 28 f.write(rest) 29 30if __name__ == '__main__': 31 main() 32