Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t逐行打印输出(变量,结束=';';)_Python - Fatal编程技术网

Python Can';t逐行打印输出(变量,结束=';';)

Python Can';t逐行打印输出(变量,结束=';';),python,Python,我为在跨平台系统上ping主机编写了一个代码 该守则的内容如下: import psutil import subprocess proc = subprocess.Popen(["ping -c 5 8.8.8.8"],shell=True) for x in range(5): getLoading = psutil.cpu_percent(interval=1) print(str(getLoading),end='<--') print('done') 64

我为在跨平台系统上ping主机编写了一个代码

该守则的内容如下:

import psutil
import subprocess

proc = subprocess.Popen(["ping -c 5 8.8.8.8"],shell=True)
for x in range(5):
    getLoading = psutil.cpu_percent(interval=1)
    print(str(getLoading),end='<--')

print('done')
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
5.0<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.5<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.1<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
3.5<--    
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.0<--
done
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
5.0<--4.5<--4.1<--3.5<--4.0<--done
导入psutil
导入子流程
proc=subprocess.Popen([“ping-c5 8.8.8.8”],shell=True)
对于范围(5)内的x:
getLoading=psutil.cpu\u百分比(间隔=1)

print(str(getLoading),end='这让我想起print函数“缓冲”一些数据,然后再写入。 我在代码中使用print时遇到了类似的问题。为了避免此类问题,我将使用记录器,这将立即打印代码

import logging
FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(
    format=FORMAT, level=logging.DEBUG, datefmt='%Y/%m/%d %H:%M:%S')
logger = logging.getLogger('MyLogger')
logger.setLevel(logging.INFO)


import psutil
import subprocess

proc = subprocess.Popen(["ping -c 5 8.8.8.8"], shell=True)
for x in range(5):
    getLoading = psutil.cpu_percent(interval=1)
    logger.info(str(getLoading) + '<--')

logger.info('done')
导入日志
格式='%(asctime)s-%(levelname)s-%(消息)s'
logging.basicConfig(
格式=格式,级别=logging.DEBUG,datefmt='%Y/%m/%d%H:%m:%S')
logger=logging.getLogger('MyLogger')
logger.setLevel(logging.INFO)
导入psutil
导入子流程
proc=subprocess.Popen([“ping-c5 8.8.8.8”],shell=True)
对于范围(5)内的x:
getLoading=psutil.cpu\u百分比(间隔=1)

info(str(getLoading)+'您需要从进程的stdout读取数据,并对其进行一些控制,以便您也可以在线打印“进程负载”

示例:

#!/usr/bin/env python

from __future__ import print_function

import sys
import psutil
import subprocess

try:
    range = xrange
except NameError:
    pass


p = subprocess.Popen(["ping",  "-c", "5", "8.8.8.8"], stdout=subprocess.PIPE)

encoding = sys.getdefaultencoding()

for line in p.stdout:
    load = psutil.cpu_percent(interval=1)
    print("{0:s}{1:0.2f}<--".format(line.decode(encoding), load))
print("done")
$ ./foo.py 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
21.10<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=15.6 ms
15.40<--
64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=15.6 ms
13.20<--
64 bytes from 8.8.8.8: icmp_seq=3 ttl=52 time=15.6 ms
20.70<--
64 bytes from 8.8.8.8: icmp_seq=4 ttl=52 time=15.5 ms
19.90<--
64 bytes from 8.8.8.8: icmp_seq=5 ttl=52 time=15.6 ms
11.00<--

19.50<--
--- 8.8.8.8 ping statistics ---
17.40<--
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
12.90<--
rtt min/avg/max/mdev = 15.596/15.629/15.669/0.114 ms
16.60<--
done
!/usr/bin/env python
来自未来导入打印功能
导入系统
导入psutil
导入子流程
尝试:
范围=X范围
除名称错误外:
通过
p=子流程Popen([“ping”、“-c”、“5”、“8.8.8.8”],stdout=子流程管道)
encoding=sys.getdefaultencoding()
对于p.stdout中的行:
负载=psutil.cpu\u百分比(间隔=1)

打印(“{0:s}{1:0.2f}我立即看到您有几个错误:
对于范围(5)中的x
需要
打印(完成)
应该是
打印('done'))
嗨,斯科特。谢谢你的快速回复。这是打字错误。我已经解决了。但这不是失败结果的根本原因。我意识到,只是希望它是正确的。嗨,斯科特。我明白了。谢谢你纠正我的粗心。是的,你说得对;会有一点缓冲,所以输出会有一点延迟;但我没有我想这就是OP本身的问题:)我得到了
print(“{0:s}{1:0.2f}是的,抱歉修复了这个问题;请参阅更新。原因是
line
是一个
bytes
字符串,所以需要对它进行解码,因为我们使用的是
str
str.format()
。非常酷的詹姆斯。我不知道你为什么刚刚被-1'D,但是因为教了我一些很酷的东西而被+1。我希望被否决的人能多给我半分钟来修正这个错误(该死!)@JamesMills,James,你知道,这是一个问题,所以-你试图发布你的答案,第一个获得更多声誉的答案,但你没有在发布之前验证你的代码。很多人只是添加无用的小回复(我不是指你)作为第一个,然后修改为正确的。这不公平吗?