这个问题类似于How to copy files to network path or drive using Python
但是,我在Linux上并尝试将文件复制到通过samba访问的Windows共享网络.
我试过这段代码:
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")
我收到错误:sh:NET:找不到
我认为这是因为’NET USE’是特定于Windows的.我如何在Linux中做类似的事情?
谢谢!
Harmaini
解决方法:
在linux上你会使用smbmount做同样的事情,就像在这里使用.NET一样.