

def patch_shebang_line(fname, pad=b' ', to_movable=True, targetdir=''):
    'Remove absolute path to python.exe in shebang lines, or re-add it'
    import re
    import sys
    import os
    target_dir = targetdir
    if (to_movable == False):
        target_dir = os.path.abspath(os.path.dirname(fname))
        target_dir = (os.path.abspath(os.path.join(target_dir, '..')) + '\\')
    executable = sys.executable
    if (sys.version_info[0] == 2):
        shebang_line = re.compile('(#!.*pythonw?\\.exe)')
    else:
        shebang_line = re.compile(b'(#!.*pythonw?\\.exe)')
        target_dir = target_dir.encode('utf-8')
    with open(fname, 'rb') as fh:
        initial_content = fh.read()
        fh.close
        fh = None
    content = shebang_line.split(initial_content, maxsplit=1)
    if (len(content) != 3):
        return
    exe = os.path.basename(content[1][2:])
    content[1] = ((b'#!' + target_dir) + exe)
    final_content = b''.join(content)
    if (initial_content == final_content):
        return
    try:
        with open(fname, 'wb') as fo:
            fo.write(final_content)
            fo.close
            fo = None
            print('patched', fname)
    except Exception:
        print('failed to patch', initial_content)

dataset/ETHPy150Open winpython/winpython/winpython/utils.py patch_shebang_line/VarMisuse@302/430 `fname`->`initial_content`