在 OSX 上使用 python 将文件复制到网络路径或驱动器

发布于 2024-09-06 05:04:13 字数 231 浏览 4 评论 0原文

我有一个类似的问题,就像这里问的问题,但我需要它在 OSX 上工作。

如何使用 Python 将文件复制到网络路径或驱动器

所以我想在 SMB 网络共享上保存文件。这可以做到吗?

谢谢!

I have a similar question like the one asked here but I need it to work on OSX.

How to copy files to network path or drive using Python

So i want to save a file on a SMB network share. Can this be done?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-09-13 05:04:17

是的,这是可以做到的。首先,通过从 Python 调用如下命令,将 SMB 网络共享挂载到本地文件系统:(

mount -t smbfs //user@server/sharename share

您可以使用 subprocess 模块来完成此操作)。 share 是 SMB 网络共享将安装到的目录的名称,我猜它必须是用户可写的。之后,您可以使用 shutdown.copyfile 复制文件。最后,您必须卸载 SMB 网络共享:

umount share

也许最好在 Python 中创建一个上下文管理器来负责安装和卸载:

from contextlib import contextmanager
import os
import shutil
import subprocess

@contextmanager
def mounted(remote_dir, local_dir):
    local_dir = os.path.abspath(local_dir)
    retcode = subprocess.call(["/sbin/mount", "-t", "smbfs", remote_dir, local_dir])
    if retcode != 0:
        raise OSError("mount operation failed")
    try:
        yield
    finally:
        retcode = subprocess.call(["/sbin/umount", local_dir])
        if retcode != 0:
            raise OSError("umount operation failed")

with mounted(remote_dir, local_dir):
    shutil.copy(file_to_be_copied, local_dir)

上面的代码片段未经测试,但它通常应该可以工作(除了语法之外)我没有注意到的错误)。另请注意,mounted 与我在其他答案中发布的 network_share_auth 上下文管理器非常相似,因此您不妨通过检查您使用的平台来将两者结合起来platform 模块,然后调用相应的命令。

Yes, it can be done. First, mount your SMB network share to the local filesystem by calling a command like this from Python:

mount -t smbfs //user@server/sharename share

(You can do it using the subprocess module). share is the name of the directory where the SMB network share will be mounted to, and I guess it has to be writable by the user. After that, you can copy the file using shutil.copyfile. Finally, you have to un-mount the SMB network share:

umount share

Probably it's the best to create a context manager in Python that takes care of mounting and unmounting:

from contextlib import contextmanager
import os
import shutil
import subprocess

@contextmanager
def mounted(remote_dir, local_dir):
    local_dir = os.path.abspath(local_dir)
    retcode = subprocess.call(["/sbin/mount", "-t", "smbfs", remote_dir, local_dir])
    if retcode != 0:
        raise OSError("mount operation failed")
    try:
        yield
    finally:
        retcode = subprocess.call(["/sbin/umount", local_dir])
        if retcode != 0:
            raise OSError("umount operation failed")

with mounted(remote_dir, local_dir):
    shutil.copy(file_to_be_copied, local_dir)

The above code snippet is not tested, but it should work in general (apart from syntax errors that I did not notice). Also note that mounted is very similar to the network_share_auth context manager I posted in my other answer, so you might as well combine the two by checking what platform you are on using the platform module and then calling the appropriate commands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文