python之read,readline,readlines区别

一个例子展示这三者区别

qd_cpu_str=os.popen(r" ps -ef|grep QD|grep -v grep|grep -v cpu|awk '{print $10','$2','$14}'")
print qd_cpu_str.read()
结果:
1 7291 QWorld
1 7297 QDBProxy
1 7310 QGameServer
1 7315 QGateway
1 7325 QCommunity
2 7390 QWorld
2 7397 QDBProxy
2 7403 QGameServer
2 7408 QGateway
2 7412 QCommunity
3 7447 QWorld
3 7454 QDBProxy
3 7475 QGameServer
3 7482 QGateway
3 7486 QCommunity


qd_cpu_str=os.popen(r" ps -ef|grep QD|grep -v grep|grep -v cpu|awk '{print $10','$2','$14}'")
for line in qd_cpu_str.read():
    print "line read is: %s" %line
结果:
/usr/bin/python2.6 /data/script/python/pycharm/pycharm_test1/cpu_QD.py
line read is: 1
line read is: 
line read is: 7
line read is: 2
line read is: 9
line read is: 1
line read is: 
line read is: Q
line read is: W
line read is: o
line read is: r
line read is: l
line read is: d
line read is:
line read is: 1
line read is: 
line read is: 7
line read is: 2
line read is: 9
line read is: 7
line read is: 
line read is: Q
line read is: D
line read is: B
line read is: P
line read is: r
line read is: o
line read is: x
line read is: y
line read is:
line read is: 1
line read is: 
line read is: 7
line read is: 3
line read is: 1
line read is: 0
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: m
line read is: e
line read is: S
line read is: e
line read is: r
line read is: v
line read is: e
line read is: r
line read is:
line read is: 1
line read is: 
line read is: 7
line read is: 3
line read is: 1
line read is: 5
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: t
line read is: e
line read is: w
line read is: a
line read is: y
line read is:
line read is: 1
line read is: 
line read is: 7
line read is: 3
line read is: 2
line read is: 5
line read is: 
line read is: Q
line read is: C
line read is: o
line read is: m
line read is: m
line read is: u
line read is: n
line read is: i
line read is: t
line read is: y
line read is:
line read is: 2
line read is: 
line read is: 7
line read is: 3
line read is: 9
line read is: 0
line read is: 
line read is: Q
line read is: W
line read is: o
line read is: r
line read is: l
line read is: d
line read is:
line read is: 2
line read is: 
line read is: 7
line read is: 3
line read is: 9
line read is: 7
line read is: 
line read is: Q
line read is: D
line read is: B
line read is: P
line read is: r
line read is: o
line read is: x
line read is: y
line read is:
line read is: 2
line read is: 
line read is: 7
line read is: 4
line read is: 0
line read is: 3
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: m
line read is: e
line read is: S
line read is: e
line read is: r
line read is: v
line read is: e
line read is: r
line read is:
line read is: 2
line read is: 
line read is: 7
line read is: 4
line read is: 0
line read is: 8
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: t
line read is: e
line read is: w
line read is: a
line read is: y
line read is:
line read is: 2
line read is: 
line read is: 7
line read is: 4
line read is: 1
line read is: 2
line read is: 
line read is: Q
line read is: C
line read is: o
line read is: m
line read is: m
line read is: u
line read is: n
line read is: i
line read is: t
line read is: y
line read is:
line read is: 3
line read is: 
line read is: 7
line read is: 4
line read is: 4
line read is: 7
line read is: 
line read is: Q
line read is: W
line read is: o
line read is: r
line read is: l
line read is: d
line read is:
line read is: 3
line read is: 
line read is: 7
line read is: 4
line read is: 5
line read is: 4
line read is: 
line read is: Q
line read is: D
line read is: B
line read is: P
line read is: r
line read is: o
line read is: x
line read is: y
line read is:
line read is: 3
line read is: 
line read is: 7
line read is: 4
line read is: 7
line read is: 5
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: m
line read is: e
line read is: S
line read is: e
line read is: r
line read is: v
line read is: e
line read is: r
line read is:
line read is: 3
line read is: 
line read is: 7
line read is: 4
line read is: 8
line read is: 2
line read is: 
line read is: Q
line read is: G
line read is: a
line read is: t
line read is: e
line read is: w
line read is: a
line read is: y
line read is:
line read is: 3
line read is: 
line read is: 7
line read is: 4
line read is: 8
line read is: 6
line read is: 
line read is: Q
line read is: C
line read is: o
line read is: m
line read is: m
line read is: u
line read is: n
line read is: i
line read is: t
line read is: y
line read is:
Process finished with exit code 0



qd_cpu_str=os.popen(r" ps -ef|grep QD|grep -v grep|grep -v cpu|awk '{print $10','$2','$14}'")
for line in qd_cpu_str.readline():
    print "line readline is: %s" %line
结果:
/usr/bin/python2.6 /data/script/python/pycharm/pycharm_test1/cpu_QD.py
line readline is: 1
line readline is: 
line readline is: 7
line readline is: 2
line readline is: 9
line readline is: 1
line readline is: 
line readline is: Q
line readline is: W
line readline is: o
line readline is: r
line readline is: l
line readline is: d
line readline is:
Process finished with exit code 0


qd_cpu_str=os.popen(r" ps -ef|grep QD|grep -v grep|grep -v cpu|awk '{print $10','$2','$14}'")
for line in qd_cpu_str.readlines():
    print "line readlines is: %s" %line
/usr/bin/python2.6 /data/script/python/pycharm/pycharm_test1/cpu_QD.py
line readlines is: 1 7291 QWorld
line readlines is: 1 7297 QDBProxy
line readlines is: 1 7310 QGameServer
line readlines is: 1 7315 QGateway
line readlines is: 1 7325 QCommunity
line readlines is: 2 7390 QWorld
line readlines is: 2 7397 QDBProxy
line readlines is: 2 7403 QGameServer
line readlines is: 2 7408 QGateway
line readlines is: 2 7412 QCommunity
line readlines is: 3 7447 QWorld
line readlines is: 3 7454 QDBProxy
line readlines is: 3 7475 QGameServer
line readlines is: 3 7482 QGateway
line readlines is: 3 7486 QCommunity
Process finished with exit code 0


你可能感兴趣的:(python,readline,readlines)