Executing a file using a Python script

Viewed 72

I don't have a clue on where to look for a solution to this. Basically i want to make a file that can be "opened with" a python script. The directory of the opened file will be stored as a variable in the script. Here's what I mean by "Open With":

Sorry if I'm not too good at describing this.

2 Answers

There might be a more direct way, but one approach could be to write a simple batch file that invokes the Python interpreter with your script:

main.bat

@echo off
python main.py %1

Add absolute or relative paths if necessary.

Within main.py, the name of the file should then be available as sys.argv[1].

If I'm not misunderstanding, what you want is your argv[1] be 'C:!PythonCode2\runfileswith\test.phktest'.

I did some test on my Windows 10, simply drag the .phktest file onto the main.py will let sys.argv[1] be the full path of that file!

Related