Skip to main content

A comprehensive toolkit for handling UNC paths, network drives, and substituted drives

Project description

UNCtools

PyPI Release Date Python 3.6+ License: MIT Platform

Windows UNC path handling and network drive utilities: convert between \\server\share and mapped drives, classify where paths come from, and probe what's actually reachable.

The Problem

On Windows, the same file often has two names: the UNC form (\\server\share\folder\file.txt) and a mapped-drive form (Z:\folder\file.txt). Tools break when handed the one they didn't expect -- network drives disconnect, subst drives masquerade as real ones, security zones silently block UNC access, and scripts that worked on one machine fail on another because the drive mappings differ.

UNCtools answers the identity questions: is this path UNC / network / subst / local -- and what is its other name? It converts between path forms using the system's live mappings, classifies path origins, probes accessibility across both name variants, and (on Windows) manages the security zones and drive mappings themselves.

[!NOTE] UNCtools is the L0 path-identity layer of the DazzleLib stack: it may probe the filesystem read-only to answer identity questions; it never mutates or transfers content (file operations live in dazzle-filekit). The public surface is locked and machine-checked -- see docs/api-stability.md.

Quick Start

pip install unctools          # or equivalently: dazzle-unctools
from unctools import convert_to_local, classify_path_origin

local = convert_to_local(r"\\server\share\project")   # -> Z:\project (if mapped)
origin = classify_path_origin(local)                  # -> "network"

Features

  • Path conversion: UNC ↔ mapped-drive translation from the system's live network mappings (win32net with net use fallback)
  • Origin classification: classify_path_origin -- unc / network / subst / local / removable / cdrom / ramdisk
  • Identity probes: existence and accessibility checks that try both name variants of a path
  • UNC path algebra: parse and build \\server\share\rest components
  • Issue detection: MAX_PATH violations, broken mappings, servers missing from the Intranet security zone
  • Windows management (optional extra): security zones, share enumeration, drive mapping create/remove
  • Cross-platform safe: imports everywhere; Windows-specific features degrade gracefully on Unix

Installation

pip install unctools              # standard (or: dazzle-unctools)
pip install unctools[windows]     # + pywin32 for the rich Windows APIs
# Development
git clone https://github.com/DazzleLib/UNCtools.git
cd UNCtools
pip install -e .[dev]

Usage

Basic Path Conversion

from unctools import convert_to_local, convert_to_unc

# Convert UNC path to local drive path
local_path = convert_to_local("\\\\server\\share\\folder\\file.txt")
# Result (if mapped): "Z:\\folder\\file.txt"

# Convert local drive path back to UNC
unc_path = convert_to_unc("Z:\\folder\\file.txt")
# Result: "\\\\server\\share\\folder\\file.txt"

Path Detection

from unctools import is_unc_path, is_network_drive, is_subst_drive, classify_path_origin

# Check if a path is a UNC path
if is_unc_path("\\\\server\\share\\file.txt"):
    print("This is a UNC path")

# Check if a drive is a network drive
if is_network_drive("Z:"):
    print("Z: is a network drive")

# Classify WHERE a path comes from
origin = classify_path_origin("C:\\Users\\")
# Result: "local", "network", "subst", "unc", etc.

Identity Probes & Batch Conversion

from unctools import file_exists, find_accessible_path, batch_convert

# Does the file exist under EITHER of its names (UNC or mapped)?
if file_exists("\\\\server\\share\\file.txt"):
    print("Reachable under at least one name")

# Which name variant actually works right now?
usable = find_accessible_path("\\\\server\\share\\file.txt")
# Result: Path("Z:\\file.txt"), Path("\\\\server\\share\\file.txt"), or None

# Convert multiple paths at once
paths = ["\\\\server\\share\\file1.txt", "\\\\server\\share\\file2.txt"]
converted = batch_convert(paths, to_unc=False)

UNC Path Algebra

from unctools import get_unc_path_elements, build_unc_path

server, share, rest = get_unc_path_elements("\\\\server\\share\\folder\\file.txt")
# Result: ("server", "share", "folder\\file.txt")

unc = build_unc_path("server", "share", "folder/file.txt")
# Result: "\\\\server\\share\\folder/file.txt"

Windows Security Zones

from unctools.windows import fix_security_zone, add_to_intranet_zone

# Fix security zone issues for a server
fix_security_zone("server")

# Add a server to the Local Intranet zone
add_to_intranet_zone("server")

Network Drive Management

from unctools.windows import create_network_mapping, remove_network_mapping

# Create a network drive mapping
success, drive = create_network_mapping("\\\\server\\share", "Z:")

# Remove a network drive mapping
remove_network_mapping("Z:")

Platform Compatibility

Platform Status
Windows Full functionality: conversion, classification, probes, security zones, drive management
Linux / macOS Path algebra and syntax checks work; mapping-dependent features degrade gracefully (no-ops / passthrough)

Requirements

  • Python 3.6+ (3.9-3.13 tested in CI)
  • pywin32 (optional, via unctools[windows]) for the rich Windows APIs -- core features fall back to net use parsing without it

Development

python -m pytest tests/   # 50+ tests; includes the api-stability import canary
black unctools
flake8 unctools

The public API surface is locked: see docs/api-stability.md for the policy, the active deprecation shims (get_path_type -> classify_path_origin, removed in 0.3.0), and known consumers. Changes follow the stack's noisy-shim deprecation policy -- never silent.

Contributing

Contributions welcome! Please open an issue or submit a pull request.

Like the project?

"Buy Me A Coffee"

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unctools-0.2.2.tar.gz (66.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unctools-0.2.2-py3-none-any.whl (40.3 kB view details)

Uploaded Python 3

File details

Details for the file unctools-0.2.2.tar.gz.

File metadata

  • Download URL: unctools-0.2.2.tar.gz
  • Upload date:
  • Size: 66.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for unctools-0.2.2.tar.gz
Algorithm Hash digest
SHA256 d6c79a5999fe68736f61f12e8481135180db1ac96c21fabac3065e9f2868733f
MD5 1ab4bb4f2fa90126395229632ce7aeae
BLAKE2b-256 69588311865aecc63db9741932944cf26a6525e07f984f6a9263414e0974d7c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for unctools-0.2.2.tar.gz:

Publisher: release.yml on DazzleLib/UNCtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unctools-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: unctools-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 40.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for unctools-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bb66b348d9b6aa28f1f328128b6b0bfab5a57eeb51778cf8c9d93f1fd8008a4e
MD5 aabf8046a8a9a1f3499cd1e1a4bb6fca
BLAKE2b-256 c842e3048ea1b9df0e4e89c94a13c5623052797e5b53da7803e3600762abc657

See more details on using hashes here.

Provenance

The following attestation bundles were made for unctools-0.2.2-py3-none-any.whl:

Publisher: release.yml on DazzleLib/UNCtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page