如何在windows中将输出直接输出到python中的txt文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4828885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:42:37  来源:igfitidea点击:

how to direct output into a txt file in python in windows

python

提问by user593908

import itertools  

variations = itertools.product('abc', repeat=3)  
for variations in variations:  
    variation_string = ""  
    for letter in variations:  
        variation_string += letter  
    print (variation_string)  

How can I redirect output into a txt file (on windows platform)?

如何将输出重定向到 txt 文件(在 Windows 平台上)?

采纳答案by Adam

If it were me, I would use David Heffernan's method above to write your variable to the text file (because other methods require the user to use a command prompt).

如果是我,我会使用上面 David Heffernan 的方法将您的变量写入文本文件(因为其他方法需要用户使用命令提示符)。

import itertools  

file = open('out.txt', 'w')
variations = itertools.product('abc', repeat=3)  
for variations in variations:  
    variation_string = ""  
    for letter in variations:  
        variation_string += letter  
    file.write(variation_string)
file.close()

回答by David Heffernan

From the console you would write:

从控制台你会写:

python script.py > out.txt

If you want to do it in Python then you would write:

如果你想用 Python 来做,那么你可以这样写:

with open('out.txt', 'w') as f:
    f.write(something)

Obviously this is just a trivial example. You'd clearly do more inside the with block.

显然,这只是一个简单的例子。你显然会在 with 块内做更多的事情。

回答by kefeizhou

In window command prompt, this command will store output of program.py into file output.txt

在窗口命令提示符下,此命令会将 program.py 的输出存储到文件 output.txt 中

python program.py > output.txt

回答by YOU

you may use >>

你可以使用 >>

log = open("test.log","w")

print >> log, variation_string

log.close()

回答by user473489

You may also redirect stdoutto your file directly in your script as printwrites by default to sys.stdoutfile handler. Python provides a simple way to to it:

您也可以stdout直接在脚本中重定向到您的文件,因为print默认情况下写入sys.stdout文件处理程序。Python 提供了一种简单的方法:

import sys  # Need to have acces to sys.stdout
fd = open('foo.txt','w') # open the result file in write mode
old_stdout = sys.stdout   # store the default system handler to be able to restore it
sys.stdout = fd # Now your file is used by print as destination 
print 'bar' # 'bar' is added to your file
sys.stdout=old_stdout # here we restore the default behavior
print 'foorbar' # this is printed on the console
fd.close() # to not forget to close your file

回答by Morse

Extension to David's answer

大卫回答的扩展

If you are using PyCharm,

如果您使用的是PyCharm

Go to Run --> Edit Configurations --> Logs --> Check mark Save console output to file --> Enter complete path --> Apply

转到运行 --> 编辑配置 --> 日志 --> 选中标记将控制台输出保存到文件 --> 输入完整路径 --> 应用

Screenshot

截屏