MacOS – Mount Windows smb shares on a Mac using Python

macosmountpython

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

Best Answer

I think, with the help of Tony Williams, I have figured it out. My mount a Windows smb share python script:

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.