Python中的输出文件
在执行脚本时,我的代码给出了我期望的输出, 但是当我试图在文本文件中打印输出时。对齐不正确 在check.txt文件中以正确格式提供 脚本输出Python中的输出文件,python,Python,在执行脚本时,我的代码给出了我期望的输出, 但是当我试图在文本文件中打印输出时。对齐不正确 在check.txt文件中以正确格式提供 脚本输出 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy() ssh.connect('hostname', username='test', password='123') sftp = ssh.open_sftp() with open('path.txt')
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy()
ssh.connect('hostname', username='test', password='123')
sftp = ssh.open_sftp()
with open('path.txt') as fp:
for line in fp:
print line
for filename in sftp.listdir(line):
if filename.endswith('.txt') or filename.endswith('.file') or filename.endswith('.xml'):
check_1 = filename
stdin, stdout, stderr = ssh.exec_command('hostname')
result1 = stdout.read().decode().splitlines()
std1in, std1out, std1err = ssh.exec_command('python -V')
result2 = std1out.read().decode().splitlines()
output_check = '{0:>25} {1:>45} {2:>30}'.format(filename,result1,result2)
print output_check
file = open("check.txt","a")
file.write(output_check)
file.close()
check.txt文件正在写入为
/home/test1/check.txt server1 python3.0
/home/test1/check1.txt server1 python3.0
试试这个
/home/test1/check.txt server1 python3.0 /home/test1/check1.txt server1 python3.0
如何使对齐方式与check.txt文件中的脚本输出完全相同您的问题的可能重复与Paramiko无关。这只是关于将行写入本地文件。请不要回答重复的问题,而是投票将其关闭。
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy()
ssh.connect('hostname', username='test', password='123')
sftp = ssh.open_sftp()
with open('path.txt') as fp:
for line in fp:
print line
for filename in sftp.listdir(line):
if filename.endswith('.txt') or filename.endswith('.file') or filename.endswith('.xml'):
check_1 = filename
stdin, stdout, stderr = ssh.exec_command('hostname')
result1 = stdout.read().decode().splitlines()
std1in, std1out, std1err = ssh.exec_command('python -V')
result2 = std1out.read().decode().splitlines()
output_check = '{0:>25} {1:>45} {2:>30}'.format(filename,result1,result2)
print output_check
file = open("check.txt","a")
file.write(output_check,'\n')
file.close()