Python: Catching an exception works outside of a function but not inside a function
问题 I have a strange problem which I can't solve myself. If I execute outside_func.py in two separate terminals, the second execution catches the BlockingIOError exception and the message is printed: outside_func.py import fcntl import time # Raise BlockingIOError if same script is already running. try: lockfile = open('lockfile', 'w') fcntl.flock(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: print('Script already running.') time.sleep(20) If I do the same with inside_func.py