3

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.

3
  • Do you want to open .phktest file with c:\some\path\phktestrunner.py or you want to open it with only python? Commented Aug 11, 2020 at 6:33
  • i want to open .phktest with the Python FILE, not Python itself. It would be nice if I could store the full path of the .phktest file being opened as a variable. i.e. C:\!PythonCode2\runfileswith\test.phktest Commented Aug 11, 2020 at 6:37
  • By Python FILE you meant a file with .py extension. Commented Aug 11, 2020 at 6:45

2 Answers 2

4

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].

Sign up to request clarification or add additional context in comments.

2 Comments

This does technically work, but I'm not able to see any Python errors, because the console closes immediately. I've tried adding /K to the .bat file but still nothing
Add pause at the end of the batch file, after the python command line.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.