import sys
#store the original stdout object
original = sys.stdout
#open a txt file to store the stdout
sys.stdout = open('stdout.txt', 'w')
#Goes to stdout.txt
print('this is a txt file')
#restore the original stdout
sys.stdout = original
#Goes to shell output
print('this is python interpreter')
您可以使用CMD:
python script.py >> out.txt
要以编程方式重定向输出,请执行以下操作: 由于python代码的输出是stdout,因此可以将其重定向到一个文件中,因此将创建一个包含输出内容的文件,请注意,建议将原始的stout存储在var中,以防以后要向shell输出某些内容: 在
更多信息:
Cannot redirect output when I run Python script on Windows using just script's name
https://stackoverflow.com/a/3021809/8094047
https://www.blog.pythonlibrary.org/2016/06/16/python-101-redirecting-stdout/
相关问题 更多 >
编程相关推荐