使用 Subprocess.Popen 在 Notepad++ 中打开文本文件 [英] Open Text File in Notepad++ using Subprocess.Popen

查看:42
本文介绍了使用 Subprocess.Popen 在 Notepad++ 中打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 subprocess.Popen 在记事本++中打开一个文本文件

I am trying to open a text file in notepad++ using subprocess.Popen

我尝试了以下代码的许多变体,但都没有奏效:

I have tried many variations of the following code, but none of them work:

 subprocess.Popen(['start notepad++.exe', 'C:\Python27\Django_Templates\QC\postits.html'])

 subprocess.Popen(['C:\Program Files (x86)\Notepad++\notepad++.exe', 'C:\Python27\Django_Templates\QC\postits.html'])

一定有办法做到这一点.

There must be a way to do this.

注意:

我想使用 subprocess.Popen 而不是 os.system 因为我想轮询以确定程序是否仍然打开并在它关闭后做一些事情.

I want to use subprocess.Popen and not os.system because I want to poll to determine whether the program is still open and do something after it closes.

所以,这是以下代码块的一部分:

So, this is part of the following code block:

process = subprocess.Popen(.....)
while process.poll() is None:
    sleep(10)
DO SOMETHING AFTER FILE CLOSES

如果 Popen 不是执行此操作的最佳方式,我愿意接受任何允许我轮询系统 (Windows) 以确定 notepad++ 是否仍处于打开状态并允许我在关闭后执行某些操作的解决方案.

If Popen is not the best way to do this, I am open to any solution that allows me to poll my system (Windows) to determine whether notepad++ is still open, and allows me to do something after it closes.

推荐答案

我认为问题在于您的路径分隔符是反斜杠,它是 Python 字符串转义字符.

I expect that the problem is that your path separators are backslashes which is the Python string escape character.

改用原始字符串:

subprocess.Popen([
    r'C:\Program Files (x86)\Notepad++\notepad++.exe', 
    r'C:\Python27\Django_Templates\QC\postits.html'
])

或者转义反斜杠:

subprocess.Popen([
    'C:\\Program Files (x86)\\Notepad++\\notepad++.exe', 
    'C:\\Python27\\Django_Templates\\QC\\postits.html'
])

或者,更好的是,使用 os.path.join 而不是硬编码的路径分隔符.

Or, even better, use os.path.join rather than hard-coded path separators.

这篇关于使用 Subprocess.Popen 在 Notepad++ 中打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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