python无法打开notepad++的文件_通过程序在Notepad ++中打开Python文件

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

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!

解决方案

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"])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值