Accessing shared smb ubuntu in python scripts
Asked Answered
C

3

7

I have a shared ubuntu drive on my network that I can access in nautilus using smb://servername/sharedfolder or smb:///sharedfolder.

I need to be able to access it python scripting from my ubuntu machine (8.10), but I can't figure out how. I tried the obvious way (same address as nautilus), but wasn't successful.

To play around, I tried printing the content of that folder with both:

Code: for file in os.listdir("smb://servername/sharedfolder") print file Both give me "no file or directory" error on that path.

I'd really appreciate help on this - thanks.

Conservatism answered 18/5, 2015 at 7:28 Comment(1)
smb is a protocol picked up my the samba driver. This is not a universal path accessible by any other applications. What Linux does is mounting the remote SMB folder to a local folder via the driver giving you access to it. Either Mount it, or find a Python library that supports samba.Jeffrey
J
6

Python can only handle local paths. Samba is a remote path read by a driver or application in your Linux system and can there for not be directly accessed from Python unless you're using a custom library like this experimental library.

You could do something similar to (make sure your user has the permission needed to mount stuff):

import os
from subprocess import Popen, PIPE, STDOUT

# Note: Try with shell=True should not be used, but it increases readability to new users from my years of teaching people Python.
process = Popen('mkdir ~/mnt && mount -t cifs //myserver_ip_address/myshare ~/mnt -o username=samb_user,noexec', shell=True, stdout=PIPE, stderr=PIPE)
while process.poll() is None:
    print(process.stdout.readline()) # For debugging purposes, in case it asks for password or anything.

print(os.listdir('~/mnt'))

Again, using shell=True is dangerous, it should be False and you should pass the command-string as a list. But for some reason it appears "complex" if you use it the way you're supposed to, so i'll write you this warning and you can choose to follow common guidelines or just use this to try the functionality out.

Here's a complete guide on how to manually mount samba. Follow this, and replace the manual steps with automated programming.

Jeffrey answered 18/5, 2015 at 7:44 Comment(0)
C
2

Python's file-handling functions like os.listdir don't take GNOME URLs like Nautilus does, they take filenames. Those aren't the same thing.

You have three basic options here:

  1. Ask GNOME to look up the URLs for you, the same way Nautilus does.
  2. Pick a Python SMB client and use that instead. There are multiple options to choose from.
  3. Use an SMB filesystem plugin like CIFS VFS to mount the remote filesystem to a mount point so you can then access its files by pathname instead of by URL.
Chosen answered 18/5, 2015 at 7:38 Comment(0)
C
0

I have mainly used the answer from "Torxed" with somes modifications:

import os, time
from subprocess import Popen, PIPE, STDOUT

process = Popen('sudo mount -t cifs //192.168.1.1/Raspi /home/pi/mnt -o vers=1.0,username=pi,password=YOUR_PASSWORD', shell=True, stdout=PIPE, stderr=PIPE)

while process.poll() is None:
    print(process.stdout.readline()) # For debugging purposes, 

# the following command will list the content of the folder 
# thus be sure to have files or folders in the shared folder to check 
# for a correct output

print(os.listdir('/home/pi/mnt'))

time.sleep(2)
process = Popen('sudo umount //192.168.1.1/Raspi', shell=True)
  1. I had to add "vers=1.0" before username (dont forget the comma)
  2. I also directly add my password, thus i avoid any prompt.
  3. if one is not sure about "which user? Pi user, Samba user?" the answer is just the linux user (Pi, for me, works well but dont forget the vers=1.0 because it's required (I don't know why)
  4. the "while" and the "sleep" may add a delay avoiding errors.
  5. I also finish the script with the umont option to corretly close. Because if you dont add this command in the python script, you can't launch/run it twice. You need to launch the umount command in the terminal in order to be able to remount it. Thus, it may be more convenient to show it directly in the python script.
Ciapha answered 15/5, 2022 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.