如何在 mac osx 上的子进程中打开文件 [英] How to open a file in subprocess on mac osx

查看:47
本文介绍了如何在 mac osx 上的子进程中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文件并等待下一条指令的执行,直到文件没有关闭.我按照链接 How to open a在 mac OSX 10.8.2 上使用 python python 编写文件,但它不起作用.

I want to open a file and wait the execution of next instruction till file is not closed. I followed the link How to open a file on mac OSX 10.8.2 in python python but it didn't work.

subprocess.call(['open','-W',FileName]) 在文本编辑器中打开文件,但仅当文本编辑器从 Dock 强制退出时才执行下一条语句,即使我关闭了打开的文件.意思是,只有在文件关闭时它才应该执行下一条语句,然后文本编辑器应该自动从停靠栏退出.
我也试过用 Popen 但没用

subprocess.call(['open','-W',FileName]) opens the file in texteditor, but executes next statement only when text-editor is quit from dock forcefully, even though I closed the opened file.Means, it should execute next statement only when file is closed, and then texteditor should automatically quit from dock.
I also tried with Popen but it didn't work

ss=subprocess.Popen("~/Downloads/DeletingDocs.txt",shell=True)
ss.communicate()   

请推荐一些方法

推荐答案

尝试将其另存为EditOneAndQuit"然后执行:

Try saving this as "EditOneAndQuit" and then do:

chmod +x EditOneAndQuit

然后从 Python 开始:

Then start that from Python:

#!/bin/bash
# Start textedit in background
open "$1" &

# Wait till textedit has zero documents open
while true
do
sleep 1
docs=`osascript -e 'tell application "textedit" to get documents'`
if [ -z "$docs" ]; then
    # Kill off poor old textedit
    osascript -e 'tell application "textedit" to quit'
exit
fi
done

首先从 shell 尝试,通过创建一个文档并编辑它:

Try it from the shell first, by creating a document and editing it:

ls > fred.txt
./OpenOneAndQuit fred.txt

当您单击红色按钮关闭文档时,您应该会看到脚本以及 textedit 退出.

you should see that the script, along with textedit, exits when you close the document by clicking the red button.

这篇关于如何在 mac osx 上的子进程中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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