Windows UNC path handling and network drive utilities
pip install unctools- Parse UNC Paths: Extract server, share, and path components
- Network Drive Detection: Identify mapped network drives
- Drive Letter Conversion: Convert between drive letters and UNC paths
- Long Path Support: Handle Windows paths >260 characters
- Cross-Platform Safe: Graceful no-ops on Unix systems
from unctools import is_unc_path, parse_unc_path, get_unc_path
# Check if path is UNC
if is_unc_path(r"\\server\share\file.txt"):
server, share, path = parse_unc_path(r"\\server\share\file.txt")
print(f"Server: {server}, Share: {share}, Path: {path}")
# Convert drive letter to UNC path
unc_path = get_unc_path("Z:\\")
# Returns: \\server\share (if Z: is mapped to a network share)from unctools import parse_unc_path
# Parse a UNC path
path = r"\\fileserver\documents\projects\report.docx"
server, share, rel_path = parse_unc_path(path)
print(f"Server: {server}") # fileserver
print(f"Share: {share}") # documents
print(f"Path: {rel_path}") # projects\report.docxfrom unctools import is_network_drive, get_network_drives
# Check if drive is network drive
if is_network_drive("Z:"):
print("Z: is a network drive")
# Get all mapped network drives
network_drives = get_network_drives()
for drive, unc_path in network_drives.items():
print(f"{drive} -> {unc_path}")from unctools import get_unc_path, get_drive_letter
# Convert drive letter to UNC
unc = get_unc_path("Z:\\folder\\file.txt")
# Returns: \\server\share\folder\file.txt
# Convert UNC to drive letter (if mapped)
drive = get_drive_letter(r"\\server\share")
# Returns: Z: (if that UNC is mapped to Z:)from unctools import normalize_long_path
# Handle paths longer than 260 characters
long_path = "C:\\" + "a" * 300 + "\\file.txt"
# Normalize to use \\?\ prefix
normalized = normalize_long_path(long_path)
# Returns: \\?\C:\aaa...aaa\file.txt
# Now can use with Windows file operations
with open(normalized, 'r') as f:
content = f.read()is_unc_path(path): Check if path is a UNC pathparse_unc_path(path): Parse UNC path into componentsis_network_drive(drive_letter): Check if drive is network-mapped
get_unc_path(path): Convert local path to UNC if possibleget_drive_letter(unc_path): Find drive letter for UNC pathnormalize_long_path(path): Add \?\ prefix for long paths
get_network_drives(): Get dictionary of all network drivesget_drive_mapping(drive_letter): Get UNC path for specific drive
Windows:
- Full functionality for all operations
- Handles UNC paths, network drives, long paths
Linux/macOS:
- All functions return safe defaults (no-op behavior)
is_unc_path()always returns Falseget_network_drives()returns empty dictionary- No exceptions raised, just graceful degradation
UNCtools is used by:
- dazzle-filekit: Windows UNC path support
- Windows-focused file management tools
- Cross-platform applications that need Windows network path handling
| Platform | Status | Notes |
|---|---|---|
| Windows | ✅ Full Support | All features available |
| Linux | Returns safe defaults | |
| macOS | Returns safe defaults | |
| Python | 3.8+ | Tested on 3.8-3.12 |
Working with Windows network paths is surprisingly complex:
- UNC paths have special syntax (
\\server\share) - Drive letters can map to network shares
- Path length limits require special handling
- Need to detect and convert between formats
UNCtools handles all of this so you don't have to.
See the main repository for contribution guidelines.
MIT License - See LICENSE for details.