Copying files to multiple comp's on a lan

D-Man dsh8290 at rit.edu
Fri Jun 1 14:25:06 EDT 2001


On Fri, Jun 01, 2001 at 07:41:45PM +0200, Henrik Berg Nielsen wrote:
<automate copying a file to multiple destinations>



This sounds more appropriate for a shell script to me.

(First install cygwin to get a (real, useful, etc) shell
 http://sources.redhat.com/cygwin)


~~~~~~~~~~~~~ smbcopy.sh ~~~~~~~~~~~~~~~~~~~~
#!/bin/bash

IFS=":"

# In this ficticious network the machines are Mach1, Mach2, and Mach3
# and all have a samba share named 'C' which is the base path for the
# destination.

# Here is the list of machines to copy to,  I assume via Samba?
MACHINES=" //Mach1/C : //Mach2/C : //Mach3/C "

for DEST in $MACHINES ; do
    # assemble the command so as not to duplicate code
    COMMAND="cp $1 $DEST/$2"

    # display the command to the user
    echo $COMMAND

    # execute the command
    $COMMAND
done
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


This could be done in Python using a list and and a for loop, then
calling os.system( )  to do the copying,  or open the source file for
reading and iteratively open the destination files for writing and
write the data.  Ex:


~~~~~~~~~~~~ smbcopy.py ~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env python

import sys
import os

# same as above
machines = [ "//Mach1/C" , "//Mach2/C" , "//Mach3/C" ]

source_file_path = sys.argv[1]
dest_path = sys.argv[2]

source_file = open( source_file_path , "rb" )
source_data = source_file.read()
source_file.close()


for dest in machines :
    dest_file = open( dest + "/" + dest_path , "wb" )
    dest_file.write( source_data )
    dest_file.close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Both of these sample programs assume that the input (command line
arguments) existed and were valid.  The scripts could be improved to
include more error handling and user feedback.

If you went with a python implementation you might want to create a
GUI for it (not trivial!) that would allow point-and-click selection
of files and destinations.

HTH,
-D





More information about the Python-list mailing list