【发布时间】:2015-11-02 03:10:13
【问题描述】:
我是 python 的新手,我是一名学生。在我们家里,每个人都有一台电脑,他们都有管理员帐户,并且他们的密码是相同的。我正在尝试制作一个程序,将一个或多个文件夹从我的 PC 发送到其他计算机。我使用 cmd 命令“net use”使用管理员帐户访问所有计算机,并使用“xcopy”将文件夹复制到其他计算机。
问题是,当我使用net use 命令时,它显示command successfully executed,但是当我尝试使用xcopy 命令时,它无法复制文件夹。当我检查代码时,对我来说似乎没有任何问题。你能帮我找到问题吗,或者我可以在不使用cmd 或使用cmd 的情况下以其他方式做到这一点吗?以及如何获取我想要将文件夹发送到的计算机的机器名称和用户名并在代码中使用它,而不是像我在我的脚本中那样手动编写它?
这是我写的脚本:
import os
import subprocess
def check(a):
checkpath="\\\\"+a+"\\c$\\users"
if os.path.exists(checkpath): # this is the function to check if the net use command worked and get access to other computer
return True
usernumber=1000 #ı adjust the usernames and machine names of the computers like "a1000,a1001 ..."
notsendto=[] #and "m4000,m4001" to make it easy to get access a stands for account m stands for machine
machinenames=["m4001","m4002","m4003","m4004","m4005","m4006","m4007"]
path=input("write the path that you want to send: ")
for i in machinenames:
usernumber+=1
subprocess.Popen("net use \\\\"+i+"\\c$ /user:"+i+"\\administrator P.a$$word /p:yes")
if check(i):
subprocess.Popen("xcopy "+path+" \\\\"+i+"\\c$\\users\\"+str(usernumber)+"\\desktop\\Incoming!!! /s /i")
else:
print("could not send to"+i)
【问题讨论】:
-
你没有在任何地方定义
kontrol。 -
@ppperry 他的代码中还有其他问题(例如未终止的字符串文字)。我假设
kontrol是check。 -
仅供参考:您可以使用
str.format和原始字符串文字来清除其中的大部分内容。您可以使用r"net use \\{machinename}\c$ /user:{machinename}\administrator SOMEPASS /p:yes".format(machinename=i)而不是"net use \\\\" + i + "\\c$ /user:" + i + "\\administrator SOMEPASS /p:yes" -
不相关:
r'\\host\path\to'(原始字符串文字)允许避免转义反斜杠。
标签: python windows python-3.x cmd