Question

I would like create a python script that mounts a Windows smb share on a Mac then copies data to it. I am falling at the first hurdle, having problems with mounting the share. It should be really easy but I can't get it to work. I have trawled the internet and have a few clues:

mount -t smbfs //user@server/sharename share

This doesn't work but I have seen another solution that uses the os module for the mount command:

import os
os.system("mount -t smbfs //user@server/sharename share")

But this doesn't work either.

I have also read about creating a local mount point for the remote share to mount to. Is this true? And how would I go about this?

Many thanks for any insights

All the best

John

Was it helpful?

Solution

I think I have figured it out:

import os
directory = "/Users/user.name/foldername"
if not os.path.exists(directory): os.makedirs(directory)
os.system("mount_smbfs //user.name:password@server/servershare ~/foldername")

This will check whether a local folder called foldername exists and create it if doesn't. It will then mount the smb share into it using a defined username/password.

OTHER TIPS

An important thing to note is that the directory variable defined up there needs to be either absolute, or in relative reference, it can't use the ~ to denote the home drive.

Eg. This would be invalid and would cause os.path.exists() to function incorrectly: directory = "~/user.name/foldername"

A late addition I know, but I thought it was worth mentioning since it tripped me up when trying to use this. I would've made this a comment but I don't have the rep to be able to do so. Thanks :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top