我爲跨平臺系統上的主機編寫了一個代碼。無法通過print逐行輸出(var,end ='')
代碼的內容如下:
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
我得到的結果,我希望Windows7的上/ python3.4.3,但是失敗了結果在CentOS 6.5/python3.4.3上。 Linux上的結果看起來如下:
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
可以在任何蟒蛇專家幫我找出根本原因? 謝謝。
你有幾個錯誤,我立刻看到:'在範圍X(5)''需要:'和'打印(完成)'應該是'打印( '完成')' – Scott
嗨,斯科特。感謝您的回覆。這是錯字。我修好了它。但這不是失敗結果的根本原因。 – Pin
我意識到,只是希望它是正確的。 – Scott