要将Python控制台的输出重定向到文件,可以使用以下方法:
python your_script.py > output.txt with open('output.txt', 'w') as f: print('Hello, World!', file=f) import sys with open('output.txt', 'w') as f: sys.stdout = f print('Hello, World!') 无论选择哪种方法,都可以将Python控制台的输出写入文件中。