Search code examples
pythonbackupfile-lockingfilestreams

Python: Opening a file without creating a lock


I'm trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I don't want my script to prevent that by locking the file; the file should be able to still be deleted at any time during the backup.

How can I do this in Python? And, what happens? Do my objects just become null if the stream cannot be read?

Thank you! I'm somewhat new to Python.


Solution

  • As mentioned by @kindall, this is a Windows-specific issue. Unix OSes allow deleting.

    To do this in Windows, I needed to use win32file.CreateFile() to use the Windows-specific dwSharingMode flag (in Python's pywin32, it's just called shareMode).

    Rough Example:

    import msvcrt
    import os
    import win32file
    
    py_handle = win32file.CreateFile(
        'filename.txt',
        win32file.GENERIC_READ,
        win32file.FILE_SHARE_DELETE
            | win32file.FILE_SHARE_READ
            | win32file.FILE_SHARE_WRITE,
        None,
        win32file.OPEN_EXISTING,
        win32file.FILE_ATTRIBUTE_NORMAL,
        None
    )
    try:
        with os.fdopen(
            msvcrt.open_osfhandle(py_handle.handle, os.O_RDONLY)
        ) as file_descriptor:
            ... # read from `file_descriptor`
    finally:
        py_handle.Close()
    

    Note: if you need to keep the win32-file open beyond the lifetime of the file-handle object returned, you should invoke PyHandle.detach() on that handle.