Use the smbclient high-level interface
masterFor most common use cases, use the smbclient interface. It provides a high-level API that replicates Python's built-in os and os.path functions (e.g., mkdir, open_file, stat, exists).
Key features of smbclient:
- Connection Pooling: Connections are pooled and reused for the same server during the Python process lifetime.
- DFS Support: Automatically handles DFS (Distributed File System) targets and caches referrals.
- Simplified Auth: Authentication is typically only required for the first call to a server.
import smbclient
# Example usage
smbclient.mkdir(r"\\server\share\directory", username="user", password="pass")
with smbclient.open_file(r"\\server\share\directory\file.txt", mode="w") as fd:
fd.write(u"file contents")