[Tutor] Copying file to multiple machines.

Scott Widney SWidney@ci.las-vegas.nv.us
Thu, 20 Sep 2001 18:42:00 -0700


> I am not sure if the other e-mail I wrote had gotten through 
> or not, so I try again. I would like to think I will only 
> have to do this once, but I doubt it. I am looking at the 
> shutil.py right now . I thought maybe something like this 
> could work, open and read src and write to dst?  
> 
> import sys
> import os
> 
> machines = [ " Machine Names " ]
> 
> source_file_path = sys.argv[1]
> dest_path = sys.argv[2]
> 
> source_file = open( source_file_path , "C:\\stuff\\remote.bat" )
> source_data = source_file.read()
> source_file.close()
> 
> for dest in machines :
>     dest_file = open( dest + "/" + dest_path , "\\C$\\Documents and
Settings\\All Users\\Start Menu\\Programs\\Startup" )
>     dest_file.write( source_data )
>     dest_file.close()
>  
> 
> The problem I am having is how to write a, I guess list and for loop for
the machines? I have the machine names already and I believe in Perl it
would be done like: 
> 
> for my $i (1..40) {
> 
>     my $machine = 'smbig0'. sprintf('2d,$i');
> 
>     #do copying and stuff
> 
> }
> 
>  
> 
> Thank you for your suggestions.
> 
>  
> 
> James

You're on the right track. Iterating over a list of machine names and
appending a pathname and filename is what I'd do....

I tend to do things from the Interactive Prompt, so I don't normally have an
argument list to read from; but here's my test run. Notice below that you
get the [list of machine names] in three lines that are very similar to the
three lines used to get your "source_data"....

src = 'C:\\stuff\\remote.bat'
machines = 'C:\\stuff\\list-of-names.txt'
# The machines file should have one name per line,
# including the leading double backslash
dest = '\\c$\\Documents and Settings\\All Users\\Start
Menu\\Programs\\Startup\\remote.bat'

# Read in the whole source file as one big string
src_file = open(src, 'r')
src_data = src_file.read()
src_file.close()

# Next, read in the whole machines file,
# returning each line as an item in a list
machine_file = open(machines, 'r')
machine_list = machine_file.readlines()
machines.close()

# Finally, iterate through your list,
# and create your target files
for eachPC in machine_list:
    # Create the file or overwrite an existing copy
    dest_file = open(eachPC[:-1] + dest, 'w')
    # The [:-1] slice operator eliminates the
    # newline character at the end of each item.
    # Then we append the destination to the name.
    dest_file.write(src_data)
    dest_file.close()

-----
Scott