'Python 3: accessing windows share under Linux by UNC path
First, lets ensure that windows share is accesable:
$ sudo mkdir /mnt/test
Let's try mount, but it's fail:
$ 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.
But if provide dummy user/pass (i.e. point exactly 'USERNAME' and 'PASSWD'), mount success:
$ 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
Now lets try 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'
I'm trying four slashes, backslashes, combine it, with or without r
, unicode escaping (bytes(path, "utf-8").decode("unicode_escape")
), all this fail with No such file or directory
. May be reason of this fail is user/pass, but I have no imagine how add it to UNC.
ps. Also I try pysmb
library, it's work fine without user/pass. But I dont want using additional lib if it possible.
Solution 1:[1]
You must mount a UNC path on Linux. The OS has no way of understanding two backslashes except when mounting the path. So to make this automatic, just write some Python code to call the necessary Linux commands to accomplish the mount task and then refer to a file path as you normally would.
Example running Linux "ls" command from Python.
import os
os.system('ls')
Now follow one of these two methods.
https://unix.stackexchange.com/questions/18925/how-to-mount-a-device-in-linux
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |