要将Python控制台的输出重定向到文件,可以使用以下方法:
使用重定向符号 “>” 将输出重定向到文件中,例如:python your_script.py > output.txt使用Python代码在程序中将输出写入文件,例如:with open('output.txt', 'w') as f: print('Hello, World!', file=f)使用sys模块重定向标准输出,例如:import syswith open('output.txt', 'w') as f: sys.stdout = f print('Hello, World!')无论选择哪种方法,都可以将Python控制台的输出写入文件中。


