pythonpython-3.xfileload

How to handle opening a file with your program with Python?


I am currently doing a simple Tic-tac-toe project with Python. I want to develop a new feature: "Save and Load game". I can format the game into the things I want and save them to a file, then load it if the user needs it. However, all of those processes are only in my game. I wonder if I go to the "Properties" of my file (with a weird extension) then set "Open with" as my program, then how can I actually handle it and load the game? I've research on Google but look like I didn't got the correct keyword (It said things about uploading files and loading files with Python, which I've done earlier).

Please help me with this. Thank you


Solution

  • First, you need to read arguments in your python script:

    import sys
    
    save_file = sys.argv[1]
    print(f'Open save file: {save_file}')
    

    Second, you need to convert you python script to exe (you can use auto-py-to-exe)

    Finally, you can do open with with your exe file and you'll get save file path:

    Open save file: D:\PROGRAMMING\temp\so\78424804\1.save