Skip to content

Instantly share code, notes, and snippets.

@James-E-A
Last active March 6, 2025 15:41
Show Gist options
  • Save James-E-A/905e85f2606f151ad2be1f8d314f50eb to your computer and use it in GitHub Desktop.
Save James-E-A/905e85f2606f151ad2be1f8d314f50eb to your computer and use it in GitHub Desktop.
python check directory writable on Windows
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