从记事本Plus Plus和当前目录运行文件 [英] Running File from Notepad Plus Plus and Current Directory

查看:107
本文介绍了从记事本Plus Plus和当前目录运行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络上有许多示例,介绍了如何从Notepad Plus Plus(NPP)运行文件.但是它们都无法说明当前工作目录是NPP可执行文件的位置,而不是文件的位置.
通常他们会这样:

There are a number of examples on the web of how to run a file from the Notepad Plus Plus (NPP). But they all fail to account for the fact that the current working directory is the location of the NPP's executable, and not the location of the file.
Usually they go something like this:

cmd /K "$(FULL_CURRENT_PATH)" 

考虑以下Python脚本:

Consider the following Python script:

with open('somefile.txt', 'a') as file:
    file.write('Hello there.\n')

此文件将在NPP文件夹中创建,这根本不是大多数人所期望的.大多数人希望将它放在与Python文件相同的位置.

This file will be created in the NPP folder, which is not at all what most people would expect. Most people would want it in the same location as the Python file.

您还可以执行类似的操作,并且可以按预期运行,但这将您限制为仅Python文件:

You could also do something like this, and it works as expected, but this limits you to Python files only:

<Command name="Run This Python File" Ctrl="no" Alt="no" Shift="yes" Key="116">cmd /K python &quot;$(FULL_CURRENT_PATH)&quot;</Command>

我通常不想这样做,所以我不想在Python脚本中添加额外的代码来更改当前工作目录.

I would not want to add extra code to the Python script to change the current working directory, as normally this would not be needed.

我一直试图解决这个问题,并提出了以下建议.该行位于NPP文件夹中的" shortcuts.xml "中.

I have been trying to solve this and came up with the following. This line goes in "shortcuts.xml" in the NPP folder.

<Command name="Run This File" Ctrl="yes" Alt="no" Shift="no" Key="116">cmd /K &quot;cd &quot;$(CURRENT_DIRECTORY)&quot; &amp;&amp; &quot;$(FULL_CURRENT_PATH)&quot;&quot;</Command>

因此,您关闭NPP,使用另一编辑器添加此行来编辑" shortcuts.xml ",然后启动NPP. 要运行文件,请使用 Ctrl + F5 组合键.

So you shut down the NPP, edit the "shortcuts.xml" by adding this line, using another editor, then launch the NPP. To run the file, use Ctrl+F5 key combination.

这在Windows 10中有效,但在Windows XP中失败.

This works in Windows 10, but fails in Windows XP.

如何调整它以在Windows XP中工作?

How can I tweak it to work in Windows XP?

推荐答案

尝试一下:

cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)

这篇关于从记事本Plus Plus和当前目录运行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆