sys.stdout.write或print覆盖subprocess.call输出

我在全球村 2012-06-20 10:56:04
写了个小程序,但是在输出到文本时出现了问题,请大家帮忙解答下,先谢了哈!

程序:
1 import subprocess
2
3
4 import sys
5 sys.stdout = open("record.txt","w")
6
7 fp = open("record.txt","w")
8 def func1():
9 uname = "uname"
10 uname_arg = "-a"
11 tmp_str = "the system information is : " + "/n"
12 sys.stdout.write( tmp_str ) ######此处改为print函数输出,问题也一样
13 subprocess.call([uname, uname_arg],stdout=fp)

14
15 def func2():
16 diskspace = "df"
17 diskspace_arg = "-h"
18 tmp_str = "the disk information is : " + "/n"
19 sys.stdout.write( tmp_str )
20 subprocess.call([diskspace,diskspace_arg],stdout=fp)

21 def main():
22 func1()
23 func2()
24
25 if __name__ == "__main__":
26 main()
~
~
输出为:
the system information is : /nthe disk information is : /nv 28 19:23:11 UTC 2011 i686 G NU/Linux
2 Filesystem Size Used Avail Use% Mounted on
3 /dev/sda5 19G 6.3G 12G 35% /
4 none 997M 212K 996M 1% /dev
5 none 1002M 24K 1002M 1% /dev/shm
6 none 1002M 76K 1002M 1% /var/run
7 none 1002M 0 1002M 0% /var/lock
8 /dev/sda1 118M 44M 68M 40% /boot
9 /dev/sda7 98G 1.5G 92G 2% /home

请大家注意以上输出中标明的红色部分, 出现 sys.stdout.write或print覆盖subprocess.call输出 ,我把它的位置换到其它地方,仍然不能解决,为何先执行后者,再执行前者呢?
求解答
...全文
324 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
我在全球村 2012-06-21
  • 打赏
  • 举报
回复
后来的同学们要学习交流请加QQ群245477323
bugs2k 2012-06-20
  • 打赏
  • 举报
回复
zhu@ubuntu:~/python$ ./df.py
The system information is:
Linux ubuntu 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
The disk information is:
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 29G 17G 11G 62% /
udev 989M 4.0K 989M 1% /dev
tmpfs 399M 836K 398M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 997M 156K 997M 1% /run/shm
/dev/sdb1 98G 31G 68G 31% /host

zhu@ubuntu:~/python$ python -V
Python 2.7.3
zhu@ubuntu:~/python$
bugs2k 2012-06-20
  • 打赏
  • 举报
回复
#!/usr/bin/env python

import subprocess
import sys

def fun1():
args = ['uname', '-a']
print "The system information is:"
#sys.stdout.flush()
subprocess.call(args)

def fun2():
args = ['df', '-h']
print "The disk information is:"
#sys.stdout.flush()
subprocess.call(args)

def main():
fun1()
fun2()

if __name__ == '__main__':

main()
bugs2k 2012-06-20
  • 打赏
  • 举报
回复
#!/usr/bin/env python

import subprocess

def fun1():
args = ['uname', '-a']
subprocess.call(args)

def fun2():
args = ['df', '-h']
subprocess.call(args)

def main():
fun1()
fun2()

if __name__ == '__main__':

main()
我在全球村 2012-06-20
  • 打赏
  • 举报
回复
还有就是我如何修改才能让提示信息一一对应呢?
我在全球村 2012-06-20
  • 打赏
  • 举报
回复

file.flush()

Flush the internal buffer, like stdio‘s fflush(). This may be a no-op on some file-like objects.

Note

flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.
我在全球村 2012-06-20
  • 打赏
  • 举报
回复
现在的确把问题解决了,不过能有文字解释下或链接个解释log.flush()处理缓存的过程就更好了,谢谢!
bugs2k 2012-06-20
  • 打赏
  • 举报
回复
#!/usr/bin/env python

import subprocess
import sys

log = open('dfuname.txt', 'w')

def fun1():
args = ['uname', '-a']
log.write("The system information is:\n")
log.flush()
subprocess.call(args, stdout=log)

def fun2():
args = ['df', '-h']
log.write("The disk information is:\n")
log.flush()
subprocess.call(args, stdout=log)

def main():
fun1()
fun2()

if __name__ == '__main__':

main()


zhu@ubuntu:~/python$ ./df.py
zhu@ubuntu:~/python$ cat dfuname.txt
The system information is:
Linux ubuntu 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
The disk information is:
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 29G 17G 11G 61% /
udev 989M 4.0K 989M 1% /dev
tmpfs 399M 836K 398M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 997M 156K 997M 1% /run/shm
/dev/sdb1 98G 31G 68G 31% /host
zhu@ubuntu:~/python$

我在全球村 2012-06-20
  • 打赏
  • 举报
回复
第一次论坛求助,没想到回复这么快,但是我想知道我错在哪里呀,还有就是那样写没有输出到文本啦,我要输出到文本,你的那个写法我也会是输出到终端的;一旦输出到文本就会出现不匹配和覆盖呀。还是谢谢你的回答。

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧