copyfile avoiding overwrites and race conditions

manuelg at gmail.com manuelg at gmail.com
Fri Jun 16 17:47:30 EDT 2006


Here is a code fragment, where I am trying to copy a file, avoiding
overwrites and race conditions.  The filename gets a '02','03','04' etc
appended to the end if a file with that name already exists.

I know the writing of the single space is overkill, but I am surprised
I cannot find an example of this floating around the web and newsgroups

my understanding of 'os.open' and 'os.fdopen' is minimal

## start fragment

    i = 0
    while True:

        if i >= 100: raise RanOutFileNamesError(fullpath)
        i += 1

        if i > 1:
            name0 = '%s_%02i%s' % (suggested_name, i, file_extension)
        else:
            assert i == 1
            name0 = '%s%s' % (suggested_name, file_extension)
        fullpath = os.path.join(path0, name0)

        # create dummy file, force overwrite

        try:
            fd = os.open(
                fullpath,
                os.O_CREAT | os.O_EXCL | os.O_WRONLY)
        except OSError:
            continue

        # is this really necessary?
        file = os.fdopen(fd, "w")
        file.write(' ')
        file.close()

        shutil.copyfile(original_filepath, fullpath)

        return fullpath

## end fragment




More information about the Python-list mailing list