簡體   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