使用 Python 访问远程服务器上的 Windows 共享驱动器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12734961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:54:01  来源:igfitidea点击:

Accessing a Windows shared drive on a remote server in Python

pythonwindowsshared-directory

提问by Stuart

This seems like a simple problem, but I'm having trouble figuring it out.

这似乎是一个简单的问题,但我很难弄清楚。

I set up a shared folder on a remote server so that the folder shows up in the Windows Explorer as follows: \\server-name\shared-directory. I am able to read from that folder remotely and write to that folder remotely via the Windows Explorer. When I try to access that file via a python script, however, I it says the directory does not exist.

我在远程服务器上设置了一个共享文件夹,以便该文件夹在 Windows 资源管理器中显示如下:\\server-name\shared-directory。我能够远程读取该文件夹并通过 Windows 资源管理器远程写入该文件夹。但是,当我尝试通过 python 脚本访问该文件时,它说该目录不存在。

I have administrative rights to the server and gave myself access to the shared drive (I verified this by copying files via Windows Explorer).

我拥有服务器的管理权限,并允许自己访问共享驱动器(我通过 Windows 资源管理器复制文件来验证这一点)。

In python, I am accessing the drive as follows (although I tried several different ways and had no success):

在python中,我按如下方式访问驱动器(虽然我尝试了几种不同的方法但都没有成功):

os.access('\\server-name\path-to-shared-directory', os.W_OK)

Any suggestions on what I might be doing wrong?

关于我可能做错了什么的任何建议?

EDIT:

编辑:

In response to questions, I am actually trying to copy files using the script, os.access was just used to see if the directory had write access. The copy code I am using is as follows:

在回答问题时,我实际上是在尝试使用脚本复制文件,os.access 只是用于查看目录是否具有写访问权限。我使用的复制代码如下:

def copy_folder(src, dest):
    if not os.access(dest, os.W_OK):
        return(False)
    cmd = ['xcopy', '/E', '/J', '/Y', '/I', src, dest]

    p = subprocess.Popen(args=cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

    out, err = p.communicate()

    print 'out: %s' % out
    print 'err: %s' % err

    return(True)

copy_folder(my_source_directory, '\\server-name\path-to-shared-directory')

This returns 'Invalid drive specification' as an error.

这将返回“无效驱动器规范”作为错误。

采纳答案by jdi

You should have no problem accessing files via the UNC path that you are already attempting to use, but I would recommend formatting it a bit differently to help with escaping:

通过您已经尝试使用的 UNC 路径访问文件应该没有问题,但我建议对其进行稍微不同的格式化以帮助转义:

src  = r'C:\path\to\source'
dest = r'\server-name\path-to-shared-directory'

And you don't need stdin:

你不需要stdin

p = subprocess.Popen(cmd, 
        stdout=subprocess.PIPE, 
        stderr=subprocess.PIPE, 
        shell=True)

Normally you would be able to use forward slashes for paths in python, but since you are calling out to a shell command, the backslashes are still needed.

通常,您可以在 python 中对路径使用正斜杠,但是由于您调用的是 shell 命令,因此仍然需要反斜杠。