1

The problem:

I'm currently working on a programming language, which uses a simple python interpreter. The interpreter loops over every line with a bunch of if-statements. The file extension I'd like to use is .ccp. So far my progress. I want the computer to recognise .ccp files as a CalcScript file, and open it with the script.

I don't want to have a default filename which I can open using text = open("filename.idk","r").read(), I want to open a file like a 'normal' file. You double-click on the file in explorer, and it opens it in the python script. Regardless of the filename.

What I've tried:

  1. Tinkering with the default applications in settings
  2. Tinkering in regedit
  3. Converting my .py file to .exe
  4. Scouering the internet

My code:

https://github.com/AnonymousPixel/CalcScript

Sorry for bad English if there was any.

  • 1
    Your statement "I want to open a file like a 'normal' file. You double-click on the file in explorer, and it opens it in the python script. Regardless of the filename." leads me to think you need to checkout how your OS interprets file type extensions and relates the extension to an executable. That isn't a python issue. – itprorh66 Jul 19 '21 at 13:11
  • @itproth66 You're partly correct. Yes it's partly tinkering with regedit and stuff, but I need to change my script to an executable which can open files like an application like, for example MS word. – AnonymousPixel Jul 19 '21 at 19:03
  • Have you then tried related posts on this site (such as [this](https://stackoverflow.com/questions/5458048/how-can-i-make-a-python-script-standalone-executable-to-run-without-any-dependen))? – gthanop Jul 19 '21 at 19:07
  • @gthanop Yes this solves part of the problem, it makes it an executable, which I can select as 'standard program or 'open with' program. I just need to change the python script to handle a file input (I don't know how). Thanks for the help – AnonymousPixel Jul 20 '21 at 07:46
  • The arguments passed to python should be in [`sys.argv`](https://docs.python.org/3/library/sys.html#sys.argv). Create a script which reads `sys.argv`, then package it into an executable, then make this executable the default one for opening *.ccp* files. – gthanop Jul 20 '21 at 10:46
  • It worked, thanks a lot! Is there a way I can mark your comment as an answer? – AnonymousPixel Jul 21 '21 at 07:29
  • Thank you for willingness to accept this as an answer. I will write my comments as a proper answer and then, if you still want, you will be able to accept it. – gthanop Jul 21 '21 at 11:08

1 Answers1

0

Summarizing my comments on the question, you can follow the steps below to achieve what you are asking:

  1. Use sys.argv to access the command line arguments. For example the following script will just print all the arguments given to it:
import sys

print("Given arguments: ", str(sys.argv))

Let's name it myprogram.py.

You can then call it (I think) with: python myprogram.py arg1 arg2 arg3 and it will run and print the command line arguments.

Notice that the first argument (sys.argv[0]) is the script's path. So arg1 would be sys.argv[1], arg2 would be sys.argv[2] and so on.

I am saying to use sys.argv because as far as I remember double clicking a file with an extension which has a default opening program, will open that program with the file path as an argument.

  1. Next step is to package your python script to an executable. This has been already asked and answered for example here (which is a duplicate, where you can follow the question which came before it to see even more examples). I used PyInstaller to test it (and on Windows OS). My command was like:
pyinstaller myprogram.py

... and it generated some folders and files. Specifically the folder dist\myprogram contained the executable along with its dependencies. You can then run your program by double clicking on it in the dist\myprogram folder. It should pop a CLI window, printing the arguments (ie only the program's path, since we called it without any other) and immediately exit. Or you can open a CLI window and run it with a command like:

myprogram argument1 argument2 argumentN

(supposing your current working directory is dist\myprogram) and it will indeed print the arguments.

  1. Finally you have to set it up as the program which by default opens files with .ccp extension. On Windows 10, you can do this via:

    1. Open up File Explorer.
    2. Find a file with .ccp extension (or create one).
    3. Right click on it.
    4. Click on Properties on the dialog that pops up.
    5. Go to General tab (if you are not already there) on the dialog that pops up.
    6. On the Open with: section there is a button which reads Change. Click it.
    7. Select More apps at the bottom of the dialog.
    8. Make sure you have the Always use this app to open .ccp files checkbox selected.
    9. Scroll to the bottom of the dialog and click on the blue text which prompts for manually selecting the default app, which in turn pops up a file chooser. I am not running on English language so it is a bit difficult to translate it exactly (I followed some online pages to actually see the default translation for the previous steps).
    10. Select your executable as the default.
    11. Confirm your choices by selecting Ok, Apply or anything else required.

Then you will also be able I think to change this extention later via:

Settings --> Apps --> Default Apps --> Choose default apps by file type.

Some references:

  1. PyInstaller website and introductory manual.
  2. Official page for step 3.
  3. Unofficial page for step 3, a lot more detailed.
gthanop
  • 3,035
  • 2
  • 10
  • 27