jjmark15
4/3/2017 - 7:51 PM

makes python script executable on linux

makes python script executable on linux

#!/usr/bin/env python3

import os
from subprocess import call

def main():
    shebang = '#!/usr/bin/env python3\n\n'
    fname = input('Please enter filename: ')
    files = [f for f in os.listdir() if os.path.splitext(f)[1]=='.py']
    if fname in files:
        print('File found')
        call('chmod +x {0}'.format(fname), shell=True)
        print('Permission added')
        with open(fname, "r+") as f:
            data = f.read()
            f.seek(0)
            output = shebang + data
            f.write(output)
            f.truncate()
        print('Shebang added')
    else:
        print('File not found')
    input('\nEnter to close...')

main()