python分别按行读取两个文本信息,插入响应执行命令参数后面组合成多条执行命令脚本,如:
读取描述信息工号:工号1,部门信息:部门1
dsquery user -desc 工号1 | dsmod user -dept 部门1
脚本内容:
import re #正则 提取
name=[]
with open('name.txt','r') as f: #旧工号
for line in f:
name.append(list(line.strip('\n').split(','))) #去除换行符,以逗号分隔
num=[]
with open('num.txt','r') as f: #新工号
for line in f:
num.append(list(line.strip('\n').split(','))) #去除换行符,以逗号分隔
i=0
while i<len(name):
try:
#直接改的
data = 'dsquery user -desc '+''.join(re.findall(re.compile(r"'(.*?)'"),str(name[i])))+' | dsmod user -dept '+''.join(re.findall(re.compile(r"'(.*?)'"),str(num[i])))
#查询的
datachaxun0 = 'echo '+'"'+''.join(re.findall(re.compile(r"'(.*?)'"),str(name[i])))+'"'+'>>1.txt'
datachaxun = 'dsquery user -desc '+''.join(re.findall(re.compile(r"'(.*?)'"),str(name[i])))+'>>1.txt'
#直接改的
file = open('cmd.txt', 'a',encoding='utf-8')
file.writelines(data)
file.writelines("\n")
file.close()
#查询的
file = open('cmdchaxun.txt', 'a',encoding='utf-8')
file.writelines(datachaxun0+'\n')
file.writelines(datachaxun+'\n')
file.writelines('echo >>1.txt'+'\n')
file.writelines('echo >>1.txt'+'\n')
file.writelines("\n")
file.close()
i=i+1
#time.sleep(0.2)
except Exception as e:
print("发生了错误")
print(e)
print("finish")