Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 820
SilentSteel Avatar asked Jan 17 '13 21:01

SilentSteel


People also ask

What is FileLock in Python?

This package contains a single module, which implements a platform independent file lock in Python, which provides a simple way of inter-process communication: from filelock import Timeout, FileLock lock = FileLock("high_ground.

How can I open a locked file?

Right-click on the file. In the menu that appears, select Lock File. To unlock, right-click the file and select Unlock File.

How do I disable a locked file?

Expand “Shared Folders“, then select “Open Files“. Find the file that is locked, then right-click it and choose “Close Open File“.

How do you lock a code in Python?

You can use the Python Script Editor to lock a script from being viewed, edited, or deleted by certain users. To do so, select the script and then click the Lock button on the toolbar. When a script has been locked, the only users who can view or edit that script are those who have rights to edit locked items.


2 Answers

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.

like image 160
SilentSteel Avatar answered Oct 24 '22 07:10

SilentSteel


On UNIX-like OSs, including Linux, this isn't an issue. Well, some other program could write to the file at the same time you're reading it, which could cause problems (the file you are copying could end up corrupted) but this is solvable with a verification pass.

On Windows, use Volume Snapshot Service (aka Volume Shadow Copy). VSS creates a snapshot of the volume at a moment in time, and you can open files on the snapshot without locking the files on the original volume. A quick Google found a Python module for doing copies using VSS here: http://sourceforge.net/projects/pyvss/

like image 35
kindall Avatar answered Oct 24 '22 07:10

kindall