gpt4 book ai didi

python - 如何在这段代码中连接列表和字符串?

转载 作者:行者123 更新时间:2023-11-30 23:27:31 25 4
gpt4 key购买 nike

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

我正在尝试将 path1(robocopy 中的目标路径)添加到计算机中的字符串中。我确信这里有多个错误。我试图简单地将文件复制到多台电脑。我知道还有其他方法可以做到这一点,但我正在尝试学习Python。谢谢。

对原文进行编辑:当我选择“y”时,我的 while 循环不起作用

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

最佳答案

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

此外,我认为 path1 不应以 C:\ 开头。 path1 显然应该以所有计算机上存在的 Windows 共享的名称开头。

编辑:您必须转义字符串中的反斜杠。 \a special meaning in Python strings ,您必须改为编写 \\

关于python - 如何在这段代码中连接列表和字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22025579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com