winunlock is a Windows Python module that provides functionality for opening certain types of locked files in Windows, especially the ones that are usually inaccessible due to external locks (e.g. an Excel file being locked by OneDrive). The module exposes two utilities:
winunlock.openmimics Python's built-inopenbut is capable of accessing locked files, andwinunlock.monkeypatch_openare used to monkey patch Python's built-inopento allow reading locked files.
pip install winunlockimport winunlock
# winunlock.open has the same interface as open
with winunlock.open("locked_file_path.txt", "r") as f:
content = f.read()from winunlock import monkeypatch_open
import pandas as pd
with monkeypatch_open():
# open has been temporarily patched
with open("locked_file_path.txt", "r") as f:
content = f.read()
# external code can now read locked files by default
pd.load_csv("locked_file_path.txt")The testing framework used is unittest. The tests are currently dependent on a working Excel installation. You can run the tests by executing the provided test script:
python tests.pyIn order to test full compatibility with Python's open, more tests are needed.
While winunlock is designed to handle locked files gracefully, there are certain intense locks (as demonstrated in one of the tests) that it might still not be able to handle.
Contributions to improve winunlock or expand its functionalities are welcome. Please ensure that you include unit tests for any new features or changes.
This project is licensed under the MIT License - see the LICENSE file for details.
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
passThis function is a drop-in replacement for Python's built-in open, designed to work with locked files.
Parameters:
- All parameters are the same as the built-in open.
@contextlib.contextmanager
def monkeypatch_open():
passThis context manager monkey patch Python's built-in open function in order to try to open a file via winunlock.open after open throws a PermissionError. After using the context manager, the built-in open will return to its normal functionality once out of the context.