
#Python open file for writing how to#
How to PM users in here? And meh why can't I? I looked around a lot, to solve this myself. PLEASE don't punish me with lowering my reputation. P.S.: I know this is not the proper way, but I am lacking an alternative.

I guess parit圓's example still works most of the time, because of his if condition if not os.path.exists(lock_filename):īut then the whole thing is not atomic anymore.īecause the if condition might be true in two parallel processes, and then both will rename, but only one will win the renaming race. The "if dst already exists, OSError will be raised" is great for my purposes. On Windows, if dst already exists, OSError will be raised If successful, the renaming willīe an atomic operation (this is a POSIX requirement). The operation may fail on some Unix flavors if src and dstĪre on different filesystems. It will be replaced silently if the user has permission. )īut other than parit圓 says, it does NOT work the same on Linux: (I am using it to implement a locking mechanism that works across independently started processes. I have implemented something similiar to what parit圓 suggested here as an answer: ("Assuming your Python interpreter, and the. What do you suggest to me, to get through to him?

I would like to answer to parit圓 ( ) but I can neither comment directly ('You must have 50 reputation to comment'), nor do I see any way to contact him/her directly.
#Python open file for writing code#
See my GIT where I created an easy to use class 'lockbydir.DLock' for that:Īt the bottom of the readme, you find 3 GITplayers where you can see the code examples execute live in your browser! Quite cool, isn't it? :-) EDIT: I solved it myself! By using directory existence & age as a locking mechanism! Locking by file is safe only on Windows (because Linux silently overwrites), but locking by directory works perfectly both on Linux and Windows.
