In python 2.7, is it possible (and how) to in a single atomic (race free) operation:

* Open a file
	+ If it doesn't exists, create then open it.
* Acquire a exclusive lock on the file (no other process can open or delete the file)

---

**Context**: I have a single python program that will fetch files given a list of URL/md5; If a file of the list exists and it's md5 matches, it gets skipped. If not, it will be downloaded. Now, there may be multiples instances of this program processing different lists which may overlap.

[This question](https://stackoverflow.com/questions/17352655/how-do-i-atomically-create-a-locked-file-in-linux) is almost what I need to do, but in my case I need to lock the file either way to check it's md5, while preventing others from doing so as well. Also, I need not to know whether the file existed or not prior to the operation; If it is just created the file will be empty and it's md5 won't match, so it will be downloaded anyway.

I'm using this program on Linux specifically, but cross-platform solutions are welcome.

---

**EDIT:**
In the end I've solved my issue by:

* opening the file in a+b mode (not atomic, create if doesn't exists).
* Try to lock the file exclusively (advisory):
	+ If succeed, work on file.
	+ If failed, assume someone else is working on the file and skips to the next. After no more files to process, come back to check if whoever locked the file did the job right.

As it stands, the desired operation is not supported in a single atomic step, but not needed neither.