I’ve been working on a Python language tool to programmatically convert a chess game file stored in PGN format to a file of FEN strings. For example, if a PGN file amateur_expert_issaquah_2024.pgn is:
[Event "Fake Event"]
[Site "Issaquah, WA"]
[Date "2024.06.20"]
[Round "1"]
[White "Sam Amateur"]
[Black "Bob Expert"]
[Result "0-1"]
1. d4 Nf6 2. c4 e5 {Budapest Gambit} 3. d5 Bc5
4. Bg5 Ne4 5. Bxd8 Bxf2# 0-1
then the tool commands:
source_pgn = ".\\Data\\amateur_expert_issaquah_2024.pgn"
dest_fen = ".\\Data\\amateur_expert_issaquah_2024.fen"
print("Converting file " + source_pgn + " to FEN strings ")
ChessFunctions.file_pgn_to_file_fen(source_pgn, dest_fen)
produce a FEN file named amateur_expert_issaquah_2024.fen that contains:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq d3 0 1 rnbqkb1r/pppppppp/5n2/8/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 1 2 rnbqkb1r/pppppppp/5n2/8/2PP4/8/PP2PPPP/RNBQKBNR b KQkq c3 0 2 rnbqkb1r/pppp1ppp/5n2/4p3/2PP4/8/PP2PPPP/RNBQKBNR w KQkq e6 0 3 rnbqkb1r/pppp1ppp/5n2/3Pp3/2P5/8/PP2PPPP/RNBQKBNR b KQkq - 0 3 rnbqk2r/pppp1ppp/5n2/2bPp3/2P5/8/PP2PPPP/RNBQKBNR w KQkq - 1 4 rnbqk2r/pppp1ppp/5n2/2bPp1B1/2P5/8/PP2PPPP/RN1QKBNR b KQkq - 2 4 rnbqk2r/pppp1ppp/8/2bPp1B1/2P1n3/8/PP2PPPP/RN1QKBNR w KQkq - 3 5 rnbBk2r/pppp1ppp/8/2bPp3/2P1n3/8/PP2PPPP/RN1QKBNR b KQkq - 0 5 rnbBk2r/pppp1ppp/8/3Pp3/2P1n3/8/PP2PbPP/RN1QKBNR w KQkq - 0 6
The conversion tool is among some of the most complicated code I’ve ever written, so it’s a work in progress as I fix bugs and find improvements. The current version of the tool is a too long to paste into this blog post. The code is on GitHub at https://github.com/jdmccaffrey/convert-pgn-to-fen.
I recently added a function to programmatically convert multiple PGN files in a source directory to corresponding FEN files in a destination directory. Example calling code is:
# convert all .pgn files in a directory to .fen files
src_dir = "C:\\Python\\ConvertPGNtoFEN\\MiscPGN"
dest_dir = "C:\\Python\\ConvertPGNtoFEN\\MiscFEN"
print("Convert all PGN files found in " + src_dir)
print("to FEN files at " + dest_dir + "\n")
ChessFunctions.files_pgn_to_files_fen(src_dir, dest_dir)
print("Done ")
If there are five PGN files in the source directory, the output might look like:
Convert all PGN files found in C:\Python\ConvertPGNtoFEN\MiscPGN to FEN files at C:\Python\ConvertPGNtoFEN\MiscFEN euwe_colle_karlsbad_1929.pgn fischer_spassky_belgrade_1992.pgn kashdan_baker_us_championship_1942.pgn lasker_thomas_london_1912.pgn thompson_lasker_san_francisco_1902.pgn Done
The function is quite simple, and the code itself is mostly self-explanatory:
import os
@ staticmethod
def files_pgn_to_files_fen(src_dir, dest_dir):
# scan thru directory src_dir,
# fetch all .pgn files, convert to .fen files
# assumes dest_dir exists
for src_file in os.listdir(src_dir):
if src_file.endswith(".pgn"):
print(src_file)
dest_file = src_file.replace(".pgn", ".fen")
ChessFunctions.file_pgn_to_file_fen(src_dir + \
"\\" + src_file, dest_dir + "\\" + dest_file)
The point of all of this is to programmatically analyze multiple (perhaps hundreds or thousands) chess games using the Stockfish chess engine. Stockfish requires games in FEN format but chess games are usually stored in PGN format, therefore I need a way to programmatically convert PGN to FEN.
Good stuff.

Here, in my opinion, are the three strongest U.S. chess grandmasters who never won the U.S. closed championship. All three won the U.S. Open Chess Championship more than once.
Left: Isaac Kashdan (1905-1985) was one of the strongest players in the world during the 1930s. Kashdan might have become the fourth modern world champion, after Capablanca, instead of Alekhine, but world politics and economics interfered. I met him several times and he was a gracious gentleman.
Center: Rueben Fine (1914-1993) was one of my chess heroes when I was learning to play chess. He won the U.S. Open seven times and tied for first place (with Paul Keres) in the AVRO tournament of 1938 — often considered the greatest chess tournament of all time. He might have become the fifth modern world champion, after Alekhine, instead of Botvinnik, but World War II erupted.
Right: Pal Benko (1928-2019) won the U.S. Open eight times, more times than any other player. He was born in Hungary, was in World War II, and was imprisoned in a Soviet concentration camp. He was able to defect to the U.S. in 1958. His courage and determination in the face of adversity are beyond admirable.

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2026 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2026 G2E Conference
2026 iSC West Conference
You must be logged in to post a comment.