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

  • That python program does exactly the same as the command line – Mark Feb 6 '14 at 11:03

I would suggest you start setting up networking using System Preferences.

Go to System Preferences/Network/Wi-Fi (or Ethernet)/Advanced/WINS and set your Workgroup to match the Windows Workgroup

Then go to Finder/Go/Connect to Server

Try to connect using smb://servername

Once you can connect you can try to automate.

  • 1
    Thanks for your answer. I wanted the script to mount the share then unmount it, not for me to do it manually. Thanks again. – John Harris Feb 6 '14 at 11:56
up vote 1 down vote accepted

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.

The first thing to do is to try and get it working at the command line.

What happens when you try it at the command line?

The first thing you might notice is that mount is likely to ask for the password for user "user", which it can't easily do in a python script. Next is that "share" has to exist ready to be a mount point and exist in the directory that python thinks is the current working directory since you didn't give a complete path to it.

I would use mount_smbfs rather than mount. Check the manual page for this command. You would have a command line something like this :

mount smbfs //user:password@server.example.com/sharename /Volumes/Share

assuming that /Volumes/Share exists as a directory.

  • Thanks for your help. Yes, I can run the script in terminal and it mounts the smb share in the local defined share (I used ~/SharedFolder rather than /Volumes/Share). As I am authenticated to the domain, I don't get the password prompt. The mount_smbfs worked as well as mount -t smbfs. Thanks again – John Harris Feb 6 '14 at 11:54

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.