简体   繁体   中英

Python chess FileExporter and open() function don't export a game

I've been working on project in python chess playing with lichess opening explorer API. I have succeeded in doing all the hard work and finally got the PGN tree that I wanted but when I try to export my PGN tree to a text file all I am left with is empty file.

I was trying to export my PGN file using a method proposed in the docs: https://python-chess.readthedocs.io/en/latest/pgn.html#chess.pgn.FileExporter but It doesn't work. Here is some of my code:

start = df.Setup()
start.starting_position = variation

board = chess.Board(fen=start.starting_position)

study = chess.pgn.Game()
study.setup(board)

# Get the main line and primary branches
mainline_and_friends(study)

#Here I am using method proposed in the docs and left with empty file in PGN folder

new_pgn = open(f"PGN/Italian game Evans gambit {counter}", "w", encoding="utf-8")
exporter = chess.pgn.FileExporter(new_pgn)
study.accept(exporter)

Embarrassing, this block of code is performed in a loop. I should have waited until the script end. Everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM