I have two (or more) python processes running and want to create a concept similar to an [exclusion mutex](https://en.wikipedia.org/wiki/Mutual_exclusion) for a shared resource. The 'shared resource' in this case is a directory. How might I most easily/standardly/etc implement a mutex? A hidden `.lock` file that each process agrees to check and, if exists, appends their PID as a new row and then pops their PID when they have access to the file?

I basically just want to clear a directory and make sure no other process tries to read or write to it while I'm clearing it.

Is there a standard linux way of doing this? Maybe something I can just execute with a shell line from python?