Python 3:通过UNC路径在Linux下访问Windows共享

时间:2017-07-14 09:57:36

标签: python unc

首先,让我们确保可以访问Windows共享:

$ sudo mkdir /mnt/test

让我们尝试mount,但它失败了:

$ sudo mount -t cifs //192.168.0.10/work /mnt/test
mount: wrong fs type, bad option, bad superblock on //192.168.0.10/work,
   missing codepage or helper program, or other error
   (for several filesystems (e.g. nfs, cifs) you might
   need a /sbin/mount.<type> helper program)

   In some cases useful info is found in syslog - try
   dmesg | tail or so.

但是如果提供虚拟用户/传递(即完全指向'USERNAME'和'PASSWD'),则挂载成功:

$ sudo mount -t cifs -o username=USERNAME,password=PASSWD //192.168.0.10/work /mnt/test
$ ls /mnt/test/*.txt
/mnt/test/1.txt
$ umount test

现在让我们试试python:

$ python -V
Python 3.5.2+
$ python
>>> import os
>>> os.listdir(r'//192.168.0.10/work')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '//192.168.0.10/work'

我正在尝试四个斜杠,反斜杠,组合它,有或没有r,unicode转义(bytes(path, "utf-8").decode("unicode_escape")),所有这一切都失败了No such file or directory。可能是因为失败的原因是用户/通过,但我无法想象如何将其添加到UNC。

PS。我也尝试pysmb库,没有用户/通行证就可以正常工作。但如果可能的话,我不想使用额外的lib。

0 个答案:

没有答案