繁体   English   中英

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

[英]Open a Python File in Notepad++ from a Program

我试图在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')

但是,我收到此错误:

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

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

如果您的.py文件 notepad ++ 相关联,os.startfile(f, 'notepad++.exe')将适用于您(请参阅ftype )。

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

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

参考: subprocess.call()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM