reevesii
reevesii

Reputation: 101

Use Keyboard Shortcut to Open .py File in Editor Window

I am trying to open an existing .py file in the Python editor window using only a keyboard shortcut.

I have created a script that opens the file I am trying to open:

    open(r'path\to\file.py')

and created a shortcut to this script and added a keyboard shortcut in the shortcuts properties window.

When I run the shortcut, it only runs the file but does not open it in the editor window.

Upvotes: 0

Views: 857

Answers (2)

reevesii
reevesii

Reputation: 101

  • Change the preferences in Python (In IDLE, Options>Configure IDLE>General tab) so that At Startup is Open Edit Window.
  • (I changed my default program to idle.bat, but I think I could have simply used the step above instead)
  • Then make a shortcut to the .py file.
  • Change Properties in the short cut by typing a letter in the Shortcut Key cell.

Now when I push ctrl-alt-w, this file opens up in the editor window.

Upvotes: 0

ncica
ncica

Reputation: 7206

you can try with:

import os
file_path = "C:/file/path/python_file.py"
os.system('notepad.exe ' + file_path)

or:

import subprocess
file_path = "C:/file/path/python_file.py"
subprocess.call(["C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4/bin\pycharm64.exe", file_path])

Upvotes: 0

Related Questions