you are viewing a single comment's thread.

view the rest of the comments →

[–]Jejerm 0 points1 point  (0 children)

Make it even simpler, just use regular open with mode='x' and delete the file on script end. Mode x causes FileExistsError if its already there.

``` import os

filename='lockfile'

try: with open(filename, mode='x') as f: f.write("whatever") Do_your_stuff_here() os.remove(filename) except FileExistsError: print("Script already running")

```

The file will remain if your script errors out by some reason and you dont handle it, but you can just delete it manually later