What is the best way to map windows drives using Python?

Gary Willoughby picture Gary Willoughby · Aug 13, 2009 · Viewed 55.5k times · Source

What is the best way to map a network share to a windows drive using Python? This share also requires a username and password.

Answer

aqua picture aqua · Oct 4, 2013

Building off of @Anon's suggestion:

# Drive letter: M
# Shared drive path: \\shared\folder
# Username: user123
# Password: password
import subprocess

# Disconnect anything on M
subprocess.call(r'net use m: /del', shell=True)

# Connect to shared drive, use drive letter M
subprocess.call(r'net use m: \\shared\folder /user:user123 password', shell=True)

I prefer this simple approach, especially if all the information is static.