Python Linux-复制文件到windows共享盘(samba)

标签 python networking share samba smb

这个问题类似于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: not found

我认为这是因为“NET USE”是特定于 Windows 的。我如何在 Linux 中做类似的事情?

谢谢! 哈迈尼

最佳答案

在 Linux 上你会使用 smbmount做与此处使用 NET 相同的事情。

关于Python Linux-复制文件到windows共享盘(samba),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7274446/

相关文章:

C# - 允许网络文件夹访问而不允许用户通过 Windows 资源管理器访问

networking - 为什么有些交换机有上行端口?

MySQL 在两个数据库之间共享表

linux - Ubuntu 和 Mavericks 之间 cifs 挂载的权限问题

python - 删除DataFrame中 "/"之前的空白区域

python - Pandas 在特定日期之前重新采样 - 填充缺失的时间序列

python - Matplotlib 半黑半白圆

python - 使用 Python 3.1 提交登录表单

ios - 直接分享图片到 WhatsApp

python - 根据 pandas 数据框中的条件删除每组的最后一行