flipnija513
flipnija513

Reputation: 23

How can I concatenate list and string in this code?

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]) 

Upvotes: 1

Views: 793

Answers (2)

ChrisJ
ChrisJ

Reputation: 5251

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.

Upvotes: 2

jgritty
jgritty

Reputation: 11915

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.

Upvotes: 1

Related Questions