通过计算机名 以及用户名密码登录处于同一网络的计算机 复制文件
python 代码如下
import sys
import os
import pywintypes
import win32wnet
import shutil
def connAndCopy(computerName,disk,userName,password):
HOST_NAME = computerName
SHARE_NAME =disk
SHARE_FULL_NAME = os.path.sep * 2 + os.path.sep.join((HOST_NAME, SHARE_NAME))
SHARE_USER = userName
SHARE_PWD = password
def connection():
net_resource = win32wnet.NETRESOURCE()
net_resource.lpRemoteName = SHARE_FULL_NAME
flags = 0
print("Trying to create connection to: {:s}".format(SHARE_FULL_NAME))
try:
win32wnet.WNetAddConnection2(net_resource, SHARE_PWD, SHARE_USER, flags)
except pywintypes.error as e:
print(e)
else:
print("Connection Success !")
dir_path = 'my_test'
file_name ='my_test.txt'
resource_path = os.path.sep + os.path.sep.join((dir_path, file_name))
print("Trying to copy the resource path is : {:s}".format(SHARE_FULL_NAME + resource_path))
shutil.copy2(SHARE_FULL_NAME + resource_path, 'C:\\Users\\Desktop\\test.txt')
print("copy the resource file : {:s} success!".format(SHARE_FULL_NAME + resource_path))
connection()
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
connAndCopy("computer_name", "d$", "xxxxx", "xxxxxx")
运行过程中需要install的依赖,根据报错信息pip install安装指定的包
以上仅作为个人学习,如有错误,请指教。如有侵权,联系删除。谢谢