Skip to main content
 首页 » 编程设计

Python:如何将我的代码输出导出到它自己的文本文件

2025年05月04日30qlqwjy

如何将我的代码输出导出到它自己的文本文件中?当我运行我的代码时,我从中获得了大量数据。如何导出它以便我可以读取它自己的文本文件中的所有数据行。

请您参考如下方法:

你可以用python写文件

with open("out.txt", "w") as f: 
    f.write("OUTPUT") 

或者您可以使用 io 重定向将输出重定向到文件

$ python code.py > out.txt 

  1. https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
  2. https://en.wikipedia.org/wiki/Redirection_(computing)