如何将终端输出写入文件

时间:2021-11-16 14:07:27

I want to save result of script to file. Example of script:

我想将脚本的结果保存到文件中。脚本示例:

#!/usr/bin/env python
i=0
while i<10:
  a=raw_input('Write a number')
  print 'Result%s'%str(2*a)
  i+=1

And I want to save to file print value. I know that I can do that in script f=open()..., but I want to do that using output in terminal. I read that I can use module subprocess but I don't know it is correct.

我想保存到文件打印值。我知道我可以在脚本f = open()...中做到这一点,但我想使用终端输出来做到这一点。我读到我可以使用模块子进程,但我不知道它是正确的。

2 个解决方案

#1


10  

IMO this is the correct pythonic way, with-out relying on the system shell:

IMO这是正确的pythonic方式,没有依赖系统shell:

import sys
f = open("test.out", 'w')
sys.stdout = f
print "test"
f.close()

In python you can change what is the default stdout object. You just need to assign whatever you want to sys.stdout. I think the object just need to have a write method defined (not absolutely sure, though).

在python中,您可以更改默认的stdout对象。你只需要为sys.stdout分配你想要的任何东西。我认为该对象只需要定义一个write方法(但不是绝对确定)。

This would do:

这样做:

import sys
f = open("test.out", 'w')
sys.stdout = f

i=0
while i<10:
  a=raw_input('Write a number')
  print 'Result%s'%str(2*a)
  i+=1

f.close()

It's essentially the same what 0605002 suggests for systems that support syntax he uses, but realized in pure python and should be more portable.

它基本上与0605002为支持他使用的语法的系统所建议的相同,但是在纯python中实现并且应该更加可移植。

Even more pythonic way, as per comment suggestion:

更多的pythonic方式,根据评论建议:

import sys

with open("test.out", 'w') as f:
  sys.stdout = f

  i=0
  while i<10:
    a=raw_input('Write a number')
    print 'Result%s'%str(2*a)
    i+=1

Of course you can refactor your "client code" and make it a function or something.

当然,您可以重构您的“客户端代码”并使其成为一种功能。

#2


6  

You can redirect the output to a file using > in terminal:

您可以使用> in terminal将输出重定向到文件:

python your_script.py > output.txt

#1


10  

IMO this is the correct pythonic way, with-out relying on the system shell:

IMO这是正确的pythonic方式,没有依赖系统shell:

import sys
f = open("test.out", 'w')
sys.stdout = f
print "test"
f.close()

In python you can change what is the default stdout object. You just need to assign whatever you want to sys.stdout. I think the object just need to have a write method defined (not absolutely sure, though).

在python中,您可以更改默认的stdout对象。你只需要为sys.stdout分配你想要的任何东西。我认为该对象只需要定义一个write方法(但不是绝对确定)。

This would do:

这样做:

import sys
f = open("test.out", 'w')
sys.stdout = f

i=0
while i<10:
  a=raw_input('Write a number')
  print 'Result%s'%str(2*a)
  i+=1

f.close()

It's essentially the same what 0605002 suggests for systems that support syntax he uses, but realized in pure python and should be more portable.

它基本上与0605002为支持他使用的语法的系统所建议的相同,但是在纯python中实现并且应该更加可移植。

Even more pythonic way, as per comment suggestion:

更多的pythonic方式,根据评论建议:

import sys

with open("test.out", 'w') as f:
  sys.stdout = f

  i=0
  while i<10:
    a=raw_input('Write a number')
    print 'Result%s'%str(2*a)
    i+=1

Of course you can refactor your "client code" and make it a function or something.

当然,您可以重构您的“客户端代码”并使其成为一种功能。

#2


6  

You can redirect the output to a file using > in terminal:

您可以使用> in terminal将输出重定向到文件:

python your_script.py > output.txt