我已经写了一个名为reversi.py的游戏,我想为它写一个脚本来帮助测试。这个游戏是基于AI的,需要很长时间才能运行。我希望写一个脚本来运行游戏,并将结果输出到一个文件中,这样我就可以运行游戏x次,而我去做其他事情,然后再回来。我被困在试图从脚本文件中调用游戏。这是我到目前为止所知道的:
from games import *
from reversi import *
def main():
f = open('Reversi Test', 'w')
if __name__ == '__main__':
main()提前感谢!
发布于 2014-10-01 09:59:55
如果程序写入标准输出,那么只需将其重定向到其他文件即可。类似于下面的内容
import sys
from games import *
from reversi import *
def main():
N = 100
for i in range(N):
sys.stdout = open('Reversi_Test_' + str(i), 'w')
game() # call your method here
sys.stdout.close()
if __name__ == '__main__':
main()您还可以使用with语句:
from future import with_statement
import sys
from games import *
from reversi import *
def main():
N = 100
for i in range(N):
with open('Reversi_Test_' + str(i), 'w') as sys.stdout:
game() # call your method here
if __name__ == '__main__':
main()https://stackoverflow.com/questions/26132479
复制相似问题