简体   繁体   English

检查打开的文本文件

[英]Check for open text file

I'm writing a Python application with opens several text files in different threads. 我正在编写一个Python应用程序,它在不同的线程中打开了几个文本文件。 I want each thread to make some operations on the file while it is open. 我希望每个线程在打开文件时对文件进行一些操作。 When the external text editor closes the file the processing of the corresponding thread should stop. 当外部文本编辑器关闭文件时,相应线程的处理应停止。 So I need to check on whether the text file is still opened or not. 因此,我需要检查文本文件是否仍处于打开状态。

class MyThread (Thread):

def __init__(self, sf):
    Thread.__init__(self)
    self.sf = sf # sourcefile

def run (self):
    subprocess.Popen([EDITOR, self.sf])

The problem now is that subprocess opens the file in the editor and run() terminates. 现在的问题是,子进程会在编辑器中打开文件,然后run()终止。 How can I keep the run() open until I close the file externally? 如何在外部关闭文件之前保持run()打开?

Did you try the subprocess.call method? 您是否尝试过subprocess.call方法?

subprocess.call('notepad')

This waits for the command to complete before the process terminates and returns control back to the program. 这将等待命令完成,然后进程终止并将控制权返回给程序。

This is what i wrote: 这是我写的:

import subprocess from threading import Thread 从线程导入子进程import Thread

class MyThread(Thread):

    def __init__(self, sf):
        Thread.__init__(self)
        self.sf = sf

    def run(self):

        subprocess.call('notepad ' + self.sf)


def main():
    myThread = MyThread('test.txt')
    myThread.start()
    while myThread.isAlive():
        print "Still Alive"


if __name__ == '__main__':
    main()

The program keeps printing "Still Alive" till I close the notepad window. 程序一直打印“ Still Alive”,直到我关闭记事本窗口。

You have two events: FileOpen and FileClose. 您有两个事件:FileOpen和FileClose。 FileOpen happens when the editor is opened. 打开编辑器时会发生FileOpen。 FileClose is done when the editor closes? 当编辑器关闭时FileClose完成吗? Or when the editor closes the file? 还是当编辑器关闭文件时? If it's just on closing the editor then it should be straightforward to find out when the subprocess closes and then kill the worker threads. 如果只是关闭编辑器,那么应​​该很容易找出子进程何时关闭,然后终止工作线程。

Unless an editor provides a particular API, it's going to be a really nasty hack to find out if it closed the file but the editor remains open. 除非编辑器提供了特定的API,否则要确定它是否关闭了文件但编辑器仍处于打开状态,这将是一个非常讨厌的黑客。 Eg You may have to poll lsof and process the output. 例如,您可能必须轮询lsof并处理输出。

It sounds similar to what a revision control system does when you commit a file and it opens an editor for you. 这听起来与修订控制系统在提交文件时所执行的操作类似,并且会为您打开编辑器。 Maybe you should check the hg or bzr code bases. 也许您应该检查hg或bzr代码库。 Here is how mercurial handles it. 是Mercurial处理它的方式。 And here is how bazaar handles it. 是集市如何处理的。

I changed Shekar's version to work with vim and use the list as a command in the subprocess.call command (and added imports) . 我更改了Shekar的版本以与vim一起使用,并将该列表用作subprocess.call命令中的命令(并添加了import)。 It works as he describes. 如他所描述的那样工作。

from threading import Thread                                                    
import subprocess                                                               
class MyThread(Thread):                                                         
    def __init__(self, sf):                                                     
        Thread.__init__(self)                                                   
        self.sf = sf                                                            
    def run(self):                                                              
        subprocess.call(['/usr/bin/vim', self.sf])                              

def main():                                                                     
    myThread = MyThread('test.txt')                                             
    myThread.start()                                                            
    while myThread.isAlive():                                                   
        print "Still Alive"                                                     

if __name__ == '__main__':                                                      
    main()  

I found the problem. 我发现了问题。 I was calling the editor MacVim via command line with /usr/bin/mvim. 我正在通过命令行使用/ usr / bin / mvim调用编辑器MacVim。 Now I changed it to /Applications/MacVim.app/Contents/MacOS/MacVim and it works as expected. 现在,我将其更改为/Applications/MacVim.app/Contents/MacOS/MacVim,它可以按预期工作。 I think that is because it checks the only process which it handles directly, in the first case it was the terminal and not the editor. 我认为这是因为它检查直接处理的唯一进程,在第一种情况下,它是终端而不是编辑器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM