2013-03-16 98 views
2

我试图在Notepad ++中复制IDLE的alt + m命令(在sys路径中打开一个模块)。我喜欢Notepad ++进行编辑(而不是IDLE),但这是我无法找到的一个功能。从程序在记事本++中打开Python文件

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

我已经试过这样:

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'文件夹中的各个子文件夹中)。或者,我可以简单地做:

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++ 

这不占用子文件夹,似乎相当笨重。谢谢!

回答

1

如果您的.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"]) 

参考:subprocess.call()

+0

谢谢!我需要第二个,因为我从另一个Python程序打开Python('.py')文件。另外,我添加了一个链接到子流程模块。 – 2013-03-16 15:31:39

+0

另外,你知道如何找到一个文件完整的文件路径完全给定的文件名? – 2013-03-16 15:33:29

+0

os.path.abspath(path)http://docs.python.org/2/library/os.path.html但请记住真正的_solely_路径无法解析,Python运行时没有办法如果它位于几个文件夹中,则推导出foo.txt的完整路径 – 2013-03-16 15:35:05