Rename file without overwriting existing files
Cameron Simpson
cs at zip.com.au
Sun Jan 29 23:33:03 EST 2017
On 30Jan2017 13:49, Steve D'Aprano <steve+python at pearwood.info> wrote:
>This code contains a Time Of Check to Time Of Use bug:
>
> if os.path.exists(destination)
> raise ValueError('destination already exists')
> os.rename(oldname, destination)
>
>
>In the microsecond between checking for the existence of the destination and
>actually doing the rename, it is possible that another process may create
>the destination, resulting in data loss.
>
>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug?
For files this is a problem at the Python level. At the UNIX level you can play
neat games with open(2) and the various O_* modes.
however, with directories things are more cut and dry. Do you have much freedom
here? What's the wider context of the question?
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Python-list
mailing list