Reputation: 3205
I am trying to mount a smb network share onto the desktop via python, I don't want the share to be mounted in a folder, but were all the other mounted shares are (if I use 'connect to Server' in OSX I want my python mount to be mounted in the same location). Here is the current python code:
directory = os.path.expanduser('~/Desktop')
directory = os.path.normpath(directory)
os.system("mount_smbfs //server/servershare " + directory)
When I run the above, something strange happens. In finder, my home, which has the icon of a house and my username changes to the mount name, it screws it up a bit.
Upvotes: 3
Views: 9724
Reputation: 21
If you had desired to also open up a Finder window on the desktop, then this is another solution and one I wound up using myself.
import os
os.system("open -g smb://Server/Share")
#perform a timeout loop checking for finished attachment
if os.path.exists("/Volumes/Share"):
# I use a for loop around this to sleep a second and try 20 times.
# it needs to handle waiting for a server to wake from sleep and then attach
#Do my important functions
os.system("umount /Volumes/Share")
The pros are:
The cons are:
umount
-g
to leave the window behind other on the desktop, but if you want to run hidden in the background then Ivan X's solution is preferred.On the other hand, if you want a new Finder window then this works well.
Upvotes: 0
Reputation: 2195
If you want to do this this the kosher, Finder-like way, do it in AppleScript via shell via Python:
os.system("osascript -e 'mount volume \"smb://server/servershare\"'")
You don't need anything else -- there's no mount point. This is identical to choosing "Connect To Server", and the resulting volume will show up in /Volumes as expected.
If you need to specify a username and/or password, you can do so:
os.system("osascript -e 'mount volume \"smb://server/servershare\" \
as user name \"myUserName\" with password \"myPassword\"'")
If you want to do it your original way using mount_smbfs
, I think you want directory
to be a folder you create in /Volumes
, e.g. /Volumes/mySmbVolume
, though I've never tried to do it this way. As you have it written, you're replacing your actual Desktop folder with the volume you're mounting. You could, however, make a folder inside Desktop for and use that for directory
, and it might work. However, I'd do it like I wrote it to be most standard with the usual Mac way of doing things.
Upvotes: 5