Python中的输出文件

维杰
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                      

check.txt 文件写为

      /home/test1/check.txt        server1       python3.0  /home/test1/check1.txt        server1       python3.0
生活

试试这个,

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()   

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章