Last active
June 27, 2024 18:43
-
-
Save glennhefley/59cdc439d5f66f4e9e2b1120faaedd24 to your computer and use it in GitHub Desktop.
Here's a Python script that provides the functionality to discover PGN files in the current directory, list them, combine them into a single PGN file, delete the original files, and exit the program based on user prompts:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Here's a Python script that provides the functionality to discover PGN files in the current directory, list them, combine them into a single PGN file, delete the original files, and exit the program based on user prompts: | |
| Explanation: | |
| discover_pgn_files(): This function lists all files in the current directory that have the .pgn extension. | |
| list_pgn_files(pgn_files): This function prints the list of discovered PGN files. | |
| combine_pgn_files(pgn_files, output_file='combined.pgn'): This function combines all the discovered PGN files into a single output file named combined.pgn by default. | |
| delete_pgn_files(pgn_files): This function deletes the PGN files listed in the pgn_files list. | |
| main(): The main function that provides a menu for the user to choose options: combine PGN files, delete original PGN files, or exit the program. | |
| Usage: | |
| Save the script to a file, for example, pgn_manager.py. | |
| Run the script using Python: | |
| python pgn_manager.py | |
| Follow the on-screen prompts to list, combine, delete, or exit the program. | |
| ''' | |
| import os | |
| import chess.pgn | |
| def discover_pgn_files(): | |
| pgn_files = [f for f in os.listdir() if f.endswith('.pgn')] | |
| return pgn_files | |
| def list_pgn_files(pgn_files): | |
| if not pgn_files: | |
| print("No PGN files found in the current directory.") | |
| else: | |
| print("PGN files in the current directory:") | |
| for i, file in enumerate(pgn_files): | |
| print(f"{i + 1}. {file}") | |
| def combine_pgn_files(pgn_files, output_file='combined.pgn'): | |
| with open(output_file, 'w') as outfile: | |
| for fname in pgn_files: | |
| with open(fname) as infile: | |
| outfile.write(infile.read()) | |
| outfile.write('\n\n') | |
| print(f"Combined PGN files into {output_file}") | |
| def delete_pgn_files(pgn_files): | |
| for file in pgn_files: | |
| os.remove(file) | |
| print("Deleted the original PGN files.") | |
| def main(): | |
| while True: | |
| pgn_files = discover_pgn_files() | |
| list_pgn_files(pgn_files) | |
| print("\nOptions:") | |
| print("1. Combine all PGN files into one") | |
| print("2. Delete the original PGN files") | |
| print("3. Exit") | |
| choice = input("Enter your choice: ").strip() | |
| if choice == '1': | |
| if pgn_files: | |
| combine_pgn_files(pgn_files) | |
| else: | |
| print("No PGN files to combine.") | |
| elif choice == '2': | |
| if pgn_files: | |
| delete_pgn_files(pgn_files) | |
| else: | |
| print("No PGN files to delete.") | |
| elif choice == '3': | |
| print("Exiting the program.") | |
| break | |
| else: | |
| print("Invalid choice. Please enter a valid option.") | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Explanation: | |
| discover_pgn_files(): This function lists all files in the current directory that have the .pgn extension. | |
| list_pgn_files(pgn_files): This function prints the list of discovered PGN files. | |
| combine_pgn_files(pgn_files, output_file='combined.pgn'): This function combines all the discovered PGN files into a single output file named combined.pgn by default. | |
| delete_pgn_files(pgn_files): This function deletes the PGN files listed in the pgn_files list. | |
| main(): The main function that provides a menu for the user to choose options: combine PGN files, delete original PGN files, or exit the program. | |
| Usage: | |
| save as pgndb.py. | |
| Run the script using Python: | |
| python pgndb.py | |
| Follow the on-screen prompts to list, combine, delete, or exit the program. | |
| ''' | |
| import os | |
| import chess.pgn | |
| def discover_pgn_files(): | |
| pgn_files = [f for f in os.listdir() if f.endswith('.pgn')] | |
| return pgn_files | |
| def list_pgn_files(pgn_files): | |
| if not pgn_files: | |
| print("No PGN files found in the current directory.") | |
| else: | |
| print("PGN files in the current directory:") | |
| for i, file in enumerate(pgn_files): | |
| print(f"{i + 1}. {file}") | |
| def combine_pgn_files(pgn_files, output_file='combined.pgn'): | |
| with open(output_file, 'w') as outfile: | |
| for fname in pgn_files: | |
| with open(fname) as infile: | |
| outfile.write(infile.read()) | |
| outfile.write('\n\n') | |
| print(f"Combined PGN files into {output_file}") | |
| def delete_pgn_files(pgn_files): | |
| for file in pgn_files: | |
| os.remove(file) | |
| print("Deleted the original PGN files.") | |
| def main(): | |
| while True: | |
| pgn_files = discover_pgn_files() | |
| list_pgn_files(pgn_files) | |
| print("\nOptions:") | |
| print("1. Combine all PGN files into one") | |
| print("2. Delete the original PGN files") | |
| print("3. Exit") | |
| choice = input("Enter your choice: ").strip() | |
| if choice == '1': | |
| if pgn_files: | |
| combine_pgn_files(pgn_files) | |
| else: | |
| print("No PGN files to combine.") | |
| elif choice == '2': | |
| if pgn_files: | |
| delete_pgn_files(pgn_files) | |
| else: | |
| print("No PGN files to delete.") | |
| elif choice == '3': | |
| print("Exiting the program.") | |
| break | |
| else: | |
| print("Invalid choice. Please enter a valid option.") | |
| if __name__ == "__main__": | |
| main() | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a Python script to achieve the tasks you've mentioned: opening a PGN file, discovering if it holds one or many games, listing the games by number, displaying a game, and exiting. For displaying the game using Notepad++, the script will write the game to a temporary file and open it with Notepad++.
First, you need to install the
chesslibrary to handle PGN files. You can install it using pip:Now, here's the Python script:
Explanation:
Usage:
chess_pgn_viewer.py.Make sure Notepad++ is installed and accessible from the command line for the
subprocess.runto work correctly.