1
import os
from subprocess import call

computers = ['\\dc830mjpzplp', '\\dc830mjpzpva']

path1 = "C:\users\public\desktop"

print os.getcwd()

os.chdir("c:\shortcut")

print os.getcwd()

for i in computers:
call(["robocopy", "c:\shortcut", path1])

I'm trying to add path1, which is the target path in robocopy to the string in computers. I'm sure there are multiple errors here. I'm attempting to simply copy a file to multiple pcs. I know there are others ways of doing this but I'm trying to learn python. Thanks.

Edit from original: My while loop ins't working when I select 'y'

import os
from subprocess import call

computers = []

print "\n***This script will copy files or folders to specified workstations...***" +"\n"


answer = 'y'

while answer == 'y':
    print "Start by adding a computer name: type DC#xxxx" + "\n"
    name = "\\" + raw_input("> ") 
    computers.append(name)
    print "\n***Add Successful***" + "\n"
    print "Here's the list so far: ", computers 

    print "\nDo you need to add more computers?"
    print "Enter 'y' or 'n'"
    answer = raw_input("> ")

    if answer == 'n':
        print "ok" + "\n"
        break

    else:
        print "I don't understand, please check for typos"
        exit(0)



print "Where is the source directory?"
print "Path can be local or network" + "\n"
source = raw_input("> ")

print "Where is the destination path?"

destination = raw_input("> ")

print "Changing source directory from: " , os.getcwd()

print "Changing source directory to: " , source
os.chdir(source)

print os.getcwd()

for n in computers:
    path = n + destination
call(["robocopy", source, path]) 

2 Answers 2

2
for c in computers:
  path = c + path1
  # now you can use path

Also, I suppose that path1 should not start with C:\. path1 should start with the name of a Windows share present on all the computers apparently.

Edit: you must escape backslashes in your strings. \ has a special meaning in Python strings, you must write \\ instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that does make sense. I am trying to copy to C:\ of another workstation, specifically the desktop folder.
Then you must ensure that C:\ is shared on the target computer, and that you have write permissions. You will have to use a destination path of the form \\TargetComputer\ShareNameForC\...
Right. I need to add C$, I do have the rights also. Robocopy runs, but the destination outputs like this C:\dc830mjpzplp\users\public\desktop
I didn't think about adding [i] directly to the variable name. It's the simple things that I tend to overlook, not sure why that is. I learned a lot by posting this question and greatly appreciate your assistance. This is a great community and I hope to return the love in the future. I tested the code and it does exactly what I need it to. This is the foundation, I'm going to make it better. I know there are better ways of accomplishing my intention, but it takes weeks to get things done around here :) Thanks again.
1

There are numerous problems here.

This might be closer to what you are trying to do:

import os
from subprocess import call

computers = ['\\\\dc830mjpzplp', '\\\\dc830mjpzpva']

path1 = "\\c$\\users\\public\\desktop" # This might work if you are admin on the remote machine.
# path1 = "\\public" # Or maybe this?

for i in computers:
    call(["robocopy", "c:\\shortcut", i + path1])

There are a couple things I don't see here that I should. First, what is the name of the file that you are trying to copy? Second, what is the remote UNC path that you can copy to? You can't add c:\shortcut to a UNC path.

1 Comment

I am an admin, and the source path is in the robocopy line below, C:\shortcut. I appreciate the feedback, I apologize for not cleaning up the code better initially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.