[英]Open Text File in Notepad++ using Subprocess.Popen
I am trying to open a text file in notepad++ using subprocess.Popen 我正在尝试使用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. 必须有一种方法可以做到这一点。
NOTE: 注意:
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. 我想使用subprocess.Popen而不是os.system,因为我想轮询以确定程序是否仍处于打开状态,并在关闭程序后执行某些操作。
So, this is part of the following code block: 因此,这是以下代码块的一部分:
process = subprocess.Popen(.....)
while process.poll() is None:
sleep(10)
DO SOMETHING AFTER FILE CLOSES
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. 如果Popen不是执行此操作的最佳方法,那么我可以采用任何解决方案,使我能够轮询系统(Windows)以确定notepad ++是否仍处于打开状态,并允许我在关闭它之后执行某些操作。
I expect that the problem is that your path separators are backslashes which is the Python string escape character. 我希望问题在于您的路径分隔符是反斜杠,这是Python字符串转义符。
Use raw strings instead: 使用原始字符串:
subprocess.Popen([
r'C:\Program Files (x86)\Notepad++\notepad++.exe',
r'C:\Python27\Django_Templates\QC\postits.html'
])
Or escape the backslashes: 或转义反斜杠:
subprocess.Popen([
'C:\\Program Files (x86)\\Notepad++\\notepad++.exe',
'C:\\Python27\\Django_Templates\\QC\\postits.html'
])
Or, even better, use os.path.join
rather than hard-coded path separators. 或者,甚至更好的是,使用
os.path.join
而不是硬编码的路径分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.