How do I create a file in python without overwriting an existing file

前端 未结 3 1075
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 07:41

Currently I have a loop that tries to find an unused filename by adding suffixes to a filename string. Once it fails to find a file, it uses the name that failed to open a

相关标签:
3条回答
  • 2020-12-05 08:06

    Use os.open() with os.O_CREAT and os.O_EXCL to create the file. That will fail if the file already exists:

    >>> fd = os.open("x", os.O_WRONLY | os.O_CREAT | os.O_EXCL)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [Errno 17] File exists: 'x'
    

    Once you've created a new file, use os.fdopen() to turn the handle into a standard Python file object:

    >>> fd = os.open("y", os.O_WRONLY | os.O_CREAT | os.O_EXCL)
    >>> f = os.fdopen(fd, "w")  # f is now a standard Python file object
    

    Edit: From Python 3.3, the builtin open() has an x mode that means "open for exclusive creation, failing if the file already exists".

    0 讨论(0)
  • 2020-12-05 08:13

    If you have an id associated with each thread / process that tries to create the file, you could put that id in the suffix somewhere, thereby guaranteeing that no two processes can use the same file name.

    This eliminates the race condition between the processes.

    0 讨论(0)
  • 2020-12-05 08:31

    If you are concerned about a race condition, you can create a temporary file and then rename it.

    >>> import os
    >>> import tempfile
    >>> f = tempfile.NamedTemporaryFile(delete=False)
    >>> f.name
    'c:\\users\\hughdb~1\\appdata\\local\\temp\\tmpsmdl53'
    >>> f.write("Hello world")
    >>> f.close()
    >>> os.rename(f.name, r'C:\foo.txt')
    >>> if os.path.exists(r'C:\foo.txt') :
    ...     print 'File exists'
    ...
    File exists
    

    Alternatively, you can create the files using a uuid in the name. Stackoverflow item on this.

    >>> import uuid
    >>> str(uuid.uuid1())
    '64362370-93ef-11de-bf06-0023ae0b04b8'
    
    0 讨论(0)
提交回复
热议问题