Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd try mapping the share to an unused drive letter by calling the <code>NET USE</code> command using <code>os.system</code> (assuming you are on Windows):</p> <pre><code>os.system(r"NET USE P: \\ComputerName\ShareName %s /USER:%s\%s" % (password, domain_name, user_name)) </code></pre> <p>After you mapped the share to a drive letter, you can use <code>shutil.copyfile</code> to copy the file to the given drive. Finally, you should unmount the share:</p> <pre><code>os.system(r"NET USE P: /DELETE") </code></pre> <p>Of course this works only on Windows, and you will have to make sure that the drive letter P is available. You can check the return code of the <code>NET USE</code> command to see whether the mount succeeded; if not, you can try a different drive letter until you succeed.</p> <p>Since the two <code>NET USE</code> commands come in pair and the second one should always be executed when the first one was executed (even if an exception was raised somewhere in between), you might wrap these two calls in a context manager if you are using Python 2.5 or later:</p> <pre><code>from contextlib import contextmanager @contextmanager def network_share_auth(share, username=None, password=None, drive_letter='P'): """Context manager that mounts the given share using the given username and password to the given drive letter when entering the context and unmounts it when exiting.""" cmd_parts = ["NET USE %s: %s" % (drive_letter, share)] if password: cmd_parts.append(password) if username: cmd_parts.append("/USER:%s" % username) os.system(" ".join(cmd_parts)) try: yield finally: os.system("NET USE %s: /DELETE" % drive_letter) with network_share_auth(r"\\ComputerName\ShareName", username, password): shutil.copyfile("foo.txt", r"P:\foo.txt") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload