从程序在 Notepad++ 中打开 Python 文件

Rus*_*hal 5 python io file notepad++

我正在尝试在 Notepad++ 中复制 IDLE 的alt+m命令(在sys路径中打开一个模块)。我喜欢 Notepad++ 进行编辑(而不是 IDLE),但这是我找不到的一项功能。

alt+m按下时,我希望它运行一个请求模块的程序(这很简单,所以我可以做到)。我的问题是找到模块然后在 Notepad++ 中打开它,而不是简单地运行程序。另外,我希望它在 Notepad++ 的同一个实例(同一个窗口)中打开,而不是一个新实例。

我试过这个:

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

但是,我收到此错误:

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'
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

另外,给定一个字符串,例如'nums.py',我怎样才能找到它的完整路径?它将位于两个文件夹之一中:'D:\\my_stuff\\Google Drive\\Modules''C:\\Python27\Lib'(它也可能位于文件夹中的各个子'Lib'文件夹中)。或者,我可以简单地做:

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++
Run Code Online (Sandbox Code Playgroud)

这不考虑子文件夹,看起来相当笨重。谢谢!

Shm*_*Cat 5

如果您的 .py 文件将记事本 ++相关联os.startfile(f, 'notepad++.exe')则将为您工作(请参阅ftype)。

除非您想创建此关联,否则以下代码将为您打开记事本++:

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

参考: subprocess.call()