Last active
March 6, 2025 15:41
-
-
Save James-E-A/905e85f2606f151ad2be1f8d314f50eb to your computer and use it in GitHub Desktop.
python check directory writable on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_dir_writable(path='.', *, _q=None): | |
import multiprocessing as mp | |
from queue import Empty | |
import tempfile | |
if _q is None: | |
# caller | |
_q = mp.Queue() | |
p = mp.Process( | |
target=is_dir_writable, # IF YOU RENAME THE FUNCTION ENSURE YOU ALSO RENAME ITS INVOCATION HERE | |
args=[path], | |
kwargs={'_q': _q}, | |
daemon=True | |
) | |
p.start() | |
try: | |
result = _q.get(timeout=1.0) | |
except Empty: | |
p.terminate() | |
result = False | |
return result | |
else: | |
# worker | |
try: | |
with tempfile.NamedTemporaryFile(dir=path): | |
pass | |
except OSError: | |
_q.put(False) | |
else: | |
_q.put(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment