5

I am trying to replicate IDLE's alt + m command (open a module in the sys path) in Notepad++. I like Notepad++ for editing (rather than IDLE), but this is one feature I cannot find.

When alt+m is pressed, I want it to run a program that asks for a module (that's fairly straightforward, so I can do that). My problem is finding the module and then opening it in Notepad++, not simply running the program. In addition, I want it to open in the same instance (same window) in Notepad++, not a new instance.

I've tried this:

import os
f = r"D:\my_stuff\Google Drive\Modules\nums.py"
os.startfile(f, 'notepad++.exe')

However, I get this error:

Traceback (most recent call last):
  File '_filePath_', line 3, in <module>
    os.startfile(f, 'notepad++.exe')
OSError: [WinError 1155] No application is associated with the specified file for this operation: 'D:\\my_stuff\\Google Drive\\Modules\\nums.py'

How can I fix this?

Also, given a string, such as 'nums.py', how can I find the full path of it? It's going to be in one of two folders: 'D:\\my_stuff\\Google Drive\\Modules' or 'C:\\Python27\Lib' (it could also be in various subfolders in the 'Lib' folder). Alternatively, I could simply do:

try:
    fullPath = r'D:\\my_stuff\\Google Drive\\Modules\\' + f
    # method of opening file in Notepad++
except (IOError, FileNotFoundError):
    fullPath = r'C:\\Python27\\Lib\\' + f
    # open in Notepad++

This doesn't account for subfolders and seems rather clunky. Thanks!

1 Answer 1

6

If your .py files will be associated w/ notepad++ the os.startfile(f, 'notepad++.exe') will work for you (see ftype).

Unless, you would like to create this association , the following code will open notepad ++ for you:

import subprocess
subprocess.call([r"c:\Program ...Notepad++.exe", r"D:\my_stuff\Google Drive\Modules\nums.py"])

Reference: subprocess.call()

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

7 Comments

Thanks! I needed the second one because I am opening Python (.py) files from another Python program. Also, I added a link to the subprocess module.
Also, do you know how to find the full file path of a file solely given the file name?
os.path.abspath(path) docs.python.org/2/library/os.path.html but please bear in mind that truly solely paths couldn't be resolved , there is no way Python runtime could deduce the full path of foo.txt if its located in several folders
I tried doing f = r"nums.py" and then p = os.path.abspath(f); printing p results in: 'C:\\Python33\\nums.py', which isn't the actual path of my module (it's in 'my_stuff\Google Drive\Modules\nums.py')...
You can always search the folders along sys.path or append to it your directories of chice
|

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.