问题描述
我正在尝试在Notepad ++中复制IDLE的alt
+ m
命令(在sys
路径中打开一个模块).我喜欢Notepad ++进行编辑(而不是IDLE),但这是我找不到的一项功能.
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.
当按下alt+m
时,我希望它运行一个程序来请求一个模块(这很简单,所以我可以这样做).我的问题是找到模块,然后在Notepad ++中打开它,而不仅仅是运行程序.另外,我希望它在Notepad ++的同一实例(同一窗口)中打开,而不是新实例.
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.
我已经尝试过了:
import os
f = r"D:\my_stuff\Google Drive\Modules\nums.py"
os.startfile(f, 'notepad++.exe')
但是,出现此错误:
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'
我该如何解决?
另外,给定一个字符串,例如'nums.py'
,我如何找到它的完整路径?它将位于以下两个文件夹之一:'D:\\my_stuff\\Google Drive\\Modules'
或'C:\\Python27\Lib'
(也可以位于'Lib'
文件夹的各个子文件夹中).或者,我可以简单地做:
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!
推荐答案
如果将使用记事本++与您的.py文件进行关联,则os.startfile(f, 'notepad++.exe')
将为您工作(请参见 ftype ).
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"])
这篇关于通过程序在Notepad ++中打开Python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!