I've written a script that is able to accomplish the following:
- mount/unmount a remote SMB location using the subprocess module
- List the file contents of a specified directory
- copy a single file out from a remote directory to a specified destination folder.
- List subdirectories within a mounted SMB location.
I've done my best to make as much of my functionality reusable within functions, but there's a part of me that worries functions like
selectSingleFile() is getting a little convoluted.
Here is my code:
import os import shutil import subprocess from os import walk from shutil import copy2 from glob import glob def mount(remote_dir, local_dir): """Mounts a remote directory and chooses a local file in which to mount the directory against""" retcode = subprocess.call(["/sbin/mount", "-t", "smbfs", remote_dir, local_dir]) def unmount(local_dir): """Unmounts the local SMB directory""" retcode = subprocess.call(["/sbin/umount", local_dir]) def getFileContents(directory): """Creates a list of files within a directory""" f =  for (dirpath, dirnames, filenames) in walk(directory): f.extend(filenames) break return f def printChoices(local_dir): """Used to print a list of numbered list of files in a directory""" num = 1 a = getFileContents(local_dir) for item in a: print(str(num) + ".", item) num +=1 def listDirectory(directory): return glob(directory) def selectSingleFile(local_dir, destination): '''Used to get the contents of a directory and select a single file to copy to a destination --requires printChoices() and getFileContents() ''' a = getFileContents(local_dir) printChoices(local_dir) b = int(input("please select your choice: ")) copy2(local_dir+(a[b-1]), destination) ''' The following was an example usage of the functions above. It allowed for mounting of a remote file location, within the work network, and a listing/transfer of a single file within the directory. ''' local_dir = '/Users/ParanoidPenguin/smb/' #Specifies the local directory to mount destination = '/Users/ParanoidPenguin/Desktop/JPEGs'#For copy2, destination where file will be stored smb_location = '//Guest@192.168.1.167/Documents' #smb location to be mounted mount(smb_location, local_dir) selectSingleFile(local_dir, destination) input("Press any key to unmount: ") unmount(local_dir)