try:
    yield
finally:
    os.system('NET USE %s: /DELETE' % drive_letter)
with network_share_auth('\\\\ComputerName\\ShareName', username, password):
    shutil.copyfile('foo.txt', 'P:\\foo.txt')
How to copy files to network path or drive using Python