python 以下代码为什么会列表索引超出范围

import paramiko

def sftp_exec_command(host,port,username,password,command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=port, username=username, password=password)
stdin, stdout, stderr= ssh.exec_command(command)
list = []
for item in stdout.readlines():
list.append(item.strip())
return list
ssh.close()

def sftp_down_file(host,port,username,password,server_path, local_path):
try:
t = paramiko.Transport((host, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(server_path , local_path)
t.close()
except Exception as e:
print(e)
if __name__ == '__main__':
hosts_file = open('./hosts/host.info', 'r')
for line in hosts_file.readlines():
if line[0:1] == '#': continue
line = line.strip('\n')
items = line.split()
port = 22
host = items[0]
username = items[1]
password = items[2]

sftp_exec_command(host,port,username,password,"sh /root/xunjian.sh")
n = sftp_exec_command(host,port,username,password,"ls -t /root/log/ | head -1 ")

filename = "/root/log/%s" % (n[0])
print(filename)
sftp_down_file(host,port,username,password,filename, "D:/大数据数据/%s"%(n[0]))

因为del会实时地删掉list里面的内容,list就没有原来那么长了,再索引就会超出范围。好比十个苹果被他吃掉了三个,你再让他吃第九个苹果,就找不到了。
温馨提示:答案为网友推荐,仅供参考
相似回答