[英]Permission denied error in Python trying to close a window
I'm trying to close an open TextEdit window using Python. 我正在尝试使用Python关闭打开的TextEdit窗口。 I can open the window just fine using: 我可以使用以下命令打开窗口:
import subprocess
p = subprocess.call(['open', 'allen_grammar.txt'])
But when I try to close the window using 但是当我尝试使用关闭窗口时
p = subprocess.Popen( 'allen_grammar.txt')
p.terminate()
I get a permission denied error: 我收到一个权限被拒绝的错误:
p = subprocess.Popen( 'allen_grammar.txt')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1326, in _execute_child
raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied
I have full permissions on the file and I'm the admin of the computer. 我对该文件拥有完全权限,并且是计算机的管理员。
The call to Popen should be like this: 对Popen的调用应如下所示:
p = subprocess.Popen(['notepad.exe','allen_grammar.txt'])
I've used notepad since I don't have TextEdit, you can replace 'notepad.exe'
in the call to whatever text editor you want, p.terminate()
should work fine afterwards 因为没有TextEdit,所以使用了记事本,您可以在调用任意文本编辑器的过程中替换'notepad.exe'
, p.terminate()
应该可以正常工作
Not really an answer but a successful workaround. 不是真正的答案,而是成功的解决方法。 I had to use Applescript and calling Applescript from Python goes like this: 我必须使用Applescript,然后从Python调用Applescript如下:
os.system(f'''/usr/bin/osascript -e 'tell app "TextEdit" to close (every window whose name is "allen_grammar.txt")' ''')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.