使用Python,我如何访问Windows网络上的共享文件夹?

时间:2021-04-14 02:26:57

I have a file that I would like to copy from a shared folder which is in a shared folder on a different system, but on the same network. How can I access the folder/file? The usual open() method does not seem to work?

我有一个文件,我想从共享文件夹中复制,该文件夹位于不同系统上的共享文件夹中,但位于同一网络上。如何访问文件夹/文件?通常的open()方法似乎不起作用?

3 个解决方案

#1


45  

Use forward slashes to specify the UNC Path:

使用正斜杠指定UNC路径:

open('//HOST/share/path/to/file')

(if your Python client code is also running under Windows)

(如果您的Python客户端代码也在Windows下运行)

#2


19  

How did you try it? Maybe you are working with \ and omit proper escaping.

你是怎么试的?也许你正在使用\并省略正确的转义。

Instead of

open('\\HOST\share\path\to\file')

use either Johnsyweb's solution with the /s, or try one of

使用Johnsyweb的解决方案和/ s,或尝试其中一个

open(r'\\HOST\share\path\to\file')

or

open('\\\\HOST\\share\\path\\to\\file')

.

#3


0  

I had the same issue as OP but none of the current answers solved my issue so to add a slightly different answer that did work for me:

我有与OP相同的问题,但目前的答案都没有解决我的问题,所以添加一个对我有用的略有不同的答案:

Running Python 3.6.5 on a Windows Machine, I used the format

在Windows机器上运行Python 3.6.5,我使用了这种格式

r"\DriveName\then\file\path\txt.md"

so the combination of double backslashes from reading @Johnsyweb UNC link and adding the r in front as recommended solved my similar to OP's issue.

因此,阅读@Johnsyweb UNC链接并在前面添加r的双反斜率组合解决了我类似于OP的问题。

#1


45  

Use forward slashes to specify the UNC Path:

使用正斜杠指定UNC路径:

open('//HOST/share/path/to/file')

(if your Python client code is also running under Windows)

(如果您的Python客户端代码也在Windows下运行)

#2


19  

How did you try it? Maybe you are working with \ and omit proper escaping.

你是怎么试的?也许你正在使用\并省略正确的转义。

Instead of

open('\\HOST\share\path\to\file')

use either Johnsyweb's solution with the /s, or try one of

使用Johnsyweb的解决方案和/ s,或尝试其中一个

open(r'\\HOST\share\path\to\file')

or

open('\\\\HOST\\share\\path\\to\\file')

.

#3


0  

I had the same issue as OP but none of the current answers solved my issue so to add a slightly different answer that did work for me:

我有与OP相同的问题,但目前的答案都没有解决我的问题,所以添加一个对我有用的略有不同的答案:

Running Python 3.6.5 on a Windows Machine, I used the format

在Windows机器上运行Python 3.6.5,我使用了这种格式

r"\DriveName\then\file\path\txt.md"

so the combination of double backslashes from reading @Johnsyweb UNC link and adding the r in front as recommended solved my similar to OP's issue.

因此,阅读@Johnsyweb UNC链接并在前面添加r的双反斜率组合解决了我类似于OP的问题。