Skip to main content
 首页 » 编程设计

python之在 OSX 上使用 python select kqueue 来监视外部应用程序的文件创建

2025年05月04日8tuyile006

通常,将我的 1 小时长的录音 session 转码为 mp3 文件需要二十多分钟。

当 OSX 应用程序 garageband 完成写入该 mp3 文件时,我想使用 python 脚本执行一系列 python 代码。

在 python 中检测外部应用程序完成向文件写入数据并关闭该文件的最佳方法是什么?我阅读了有关 kqueue 和 epoll 的内容,但由于我没有操作系统事件检测方面的背景知识,也找不到一个很好的例子,所以我在这里要求一个例子。

我现在使用的代码执行以下操作,我正在寻找更优雅的代码。

while True: 
    try: 
        today_file = open("todays_recording.mp3","r") 
        my_custom_function_to_process_file(today_file) 
    except IOError: 
         print "File not ready yet..continuing to wait" 

请您参考如下方法:

您可以打开 lsof 并按您感兴趣的进程或文件进行过滤...