python net use_在循环中使用“net use”命令

我正在编写一个简单的python脚本,它连接到几个远程Windows机器,读取这些机器上远程文件夹的内容,然后压缩并复制在给定日期之后修改过的所有文件。在

问题是,当它连接到第一台计算机后,它没有连接到第二台计算机,“netuse”命令不起作用。

如果我通过计算机的windows命令行手动执行此操作,它确实可以工作,但不能通过python脚本。在

我没有找到任何可以帮助我的话题,我现在有点困了。。。你们知道我会做错什么吗?在

下面是我的代码(如果看起来不太整洁,我很抱歉,我是从python开始的)。在import os, subprocess, datetime, shutil

# the local destination of log files on my computer

ROOT_folder = 'C:\\Logs'

for i, IP in enumerate(list_of_IPs):

# list of names corresponding to the IPs

location = list_of_locs[i]

# create a local repository in my ROT folder for storing the logs of this remote station

try:

os.chdir(ROOT_folder + '\\' + location)

except:

os.mkdir(ROOT_folder + '\\' + location)

# The path to the logs that are stored on the remote windows machines

remote_path_to_logs = '_Temporary\\Logs'

print location

# Mapping a drive m: >>> Here I get the error at the 2nd iteration

subprocess.call(r'net use m: \\' + IP + '\c$ Password /user:Username', shell=True)

# The modification date of the most recent file I donwloaded is stored on my local computer in a txt file - here I read the date

try:

with open(ROOT_folder + '\\' + location + '\\last_mod.txt','r') as myFile:

last_file_downloaded = datetime.datetime.strptime(myFile.read(),'%Y-%m-%d %H:%M:%S')

except:

last_file_downloaded = datetime.datetime(1970,1,1)

os.chdir('M:\\' + remote_path_to_logs)

# I sort the list of files from oldest to newest

list_files = os.listdir('M:\\' + remote_path_to_logs)

list_sorted = sorted([(fl, os.path.getmtime(fl)) for fl in list_files],key=lambda x: x[1])

for log, logtime in list_sorted:

date_file = datetime.datetime(1970,1,1) + datetime.timedelta(seconds=logtime)

# I zip and move the file to my computer if it was modified after the date I stored on my computer

if date_file > last_file_downloaded :

print log + ': zipping and moving to local directory... '

with zipfile.ZipFile(date_file.strftime('%Y-%m-%d_%H-%M-%S') + '.zip','w', zipfile.ZIP_DEFLATED) as z:

z.write(log)

shutil.move(date_file.strftime('%Y-%m-%d_%H-%M-%S') + '.zip',ROOT_folder + '\\' + location)

# I overwrite the modification date in my file

with open(ROOT_folder + '\\' + location + '\\last_mod.txt','w') as myFile:

myFile.write(date_file.strftime('%Y-%m-%d %H:%M:%S'))

# Disconnecting the drive m:

subprocess.call('net use m: /delete /yes', shell=True)

# I tried to put a time.sleep(5) here but it does not help

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值