• <small id='OZaw0'></small><noframes id='OZaw0'>

    <legend id='OZaw0'><style id='OZaw0'><dir id='OZaw0'><q id='OZaw0'></q></dir></style></legend>

        <bdo id='OZaw0'></bdo><ul id='OZaw0'></ul>
      <tfoot id='OZaw0'></tfoot>

      1. <i id='OZaw0'><tr id='OZaw0'><dt id='OZaw0'><q id='OZaw0'><span id='OZaw0'><b id='OZaw0'><form id='OZaw0'><ins id='OZaw0'></ins><ul id='OZaw0'></ul><sub id='OZaw0'></sub></form><legend id='OZaw0'></legend><bdo id='OZaw0'><pre id='OZaw0'><center id='OZaw0'></center></pre></bdo></b><th id='OZaw0'></th></span></q></dt></tr></i><div id='OZaw0'><tfoot id='OZaw0'></tfoot><dl id='OZaw0'><fieldset id='OZaw0'></fieldset></dl></div>

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

        How to copy files to network path or drive using Python(如何使用 Python 将文件复制到网络路径或驱动器)
          <tbody id='lcjmi'></tbody>

          <legend id='lcjmi'><style id='lcjmi'><dir id='lcjmi'><q id='lcjmi'></q></dir></style></legend>
                <bdo id='lcjmi'></bdo><ul id='lcjmi'></ul>
              • <small id='lcjmi'></small><noframes id='lcjmi'>

                <i id='lcjmi'><tr id='lcjmi'><dt id='lcjmi'><q id='lcjmi'><span id='lcjmi'><b id='lcjmi'><form id='lcjmi'><ins id='lcjmi'></ins><ul id='lcjmi'></ul><sub id='lcjmi'></sub></form><legend id='lcjmi'></legend><bdo id='lcjmi'><pre id='lcjmi'><center id='lcjmi'></center></pre></bdo></b><th id='lcjmi'></th></span></q></dt></tr></i><div id='lcjmi'><tfoot id='lcjmi'></tfoot><dl id='lcjmi'><fieldset id='lcjmi'></fieldset></dl></div>
                <tfoot id='lcjmi'></tfoot>

                  本文介绍了如何使用 Python 将文件复制到网络路径或驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的问题与此类似.

                  如何使用变量将文件从网络共享复制到本地磁盘?

                  唯一的区别是我的网络驱动器使用用户名和密码进行密码保护.

                  The only difference is my network drive has a password protect with username and password.

                  我需要使用 Python 将文件复制到 Samba 共享并进行验证.

                  I need to copy files to a Samba share using Python and verify it.

                  如果我手动登录,则代码可以正常工作,但如果不登录 shutil 命令将无法正常工作.

                  If I manually login in then the code works, but without logging in the shutil command does not work.

                  推荐答案

                  我会尝试通过使用 os.system<调用 NET USE 命令将共享映射到未使用的驱动器号/code>(假设你在 Windows 上):

                  I'd try mapping the share to an unused drive letter by calling the NET USE command using os.system (assuming you are on Windows):

                  os.system(r"NET USE P: \ComputerNameShareName %s /USER:%s\%s" % (password, domain_name, user_name))
                  

                  将共享映射到驱动器号后,您可以使用 shutil.copyfile 将文件复制到给定驱动器.最后,您应该卸载共享:

                  After you mapped the share to a drive letter, you can use shutil.copyfile to copy the file to the given drive. Finally, you should unmount the share:

                  os.system(r"NET USE P: /DELETE")
                  

                  当然,这仅适用于 Windows,您必须确保驱动器号 P 可用.可以查看NET USE命令的返回码看是否挂载成功;如果没有,您可以尝试不同的驱动器号,直到成功为止.

                  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 NET USE command to see whether the mount succeeded; if not, you can try a different drive letter until you succeed.

                  由于两个 NET USE 命令是成对出现的,并且第二个应该总是在第一个执行时执行(即使在两者之间的某个地方引发了异常),您可以将这两个包装起来如果您使用的是 Python 2.5 或更高版本,则在上下文管理器中调用:

                  Since the two NET USE 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:

                  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"\ComputerNameShareName", username, password):
                       shutil.copyfile("foo.txt", r"P:foo.txt")
                  

                  这篇关于如何使用 Python 将文件复制到网络路径或驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                1. <tfoot id='7aYZK'></tfoot>
                2. <small id='7aYZK'></small><noframes id='7aYZK'>

                    <bdo id='7aYZK'></bdo><ul id='7aYZK'></ul>
                        <tbody id='7aYZK'></tbody>
                        • <i id='7aYZK'><tr id='7aYZK'><dt id='7aYZK'><q id='7aYZK'><span id='7aYZK'><b id='7aYZK'><form id='7aYZK'><ins id='7aYZK'></ins><ul id='7aYZK'></ul><sub id='7aYZK'></sub></form><legend id='7aYZK'></legend><bdo id='7aYZK'><pre id='7aYZK'><center id='7aYZK'></center></pre></bdo></b><th id='7aYZK'></th></span></q></dt></tr></i><div id='7aYZK'><tfoot id='7aYZK'></tfoot><dl id='7aYZK'><fieldset id='7aYZK'></fieldset></dl></div>
                        • <legend id='7aYZK'><style id='7aYZK'><dir id='7aYZK'><q id='7aYZK'></q></dir></style></legend>