简体   繁体   English

尝试关闭窗口的Python中的权限被拒绝错误

[英]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.

相关问题 尝试在python中关闭和打开当前对话框时出错 - Getting an error while trying to close and open current dialog window in python 尝试运行Python软件包时权限被拒绝 - Permission Denied while trying to run a Python package 尝试使用python打开文件时权限被拒绝 - permission denied while trying to open a file with python “权限被拒绝”试图在 Windows 10 上运行 Python - "Permission Denied" trying to run Python on Windows 10 Python Interactive Window VS 代码中的错误 - 无法写入文件 default.profraw:权限被拒绝 - Python Interactive Window Error in VS Code - Failed to write file default.profraw: Permission denied 为什么在尝试使用 openpyxl python 读取单元格值时出现“权限被拒绝错误”? - Why am I getting a “Permission denied error” while trying to read a cell value using openpyxl python? PermissionError: [Errno 13] 权限被拒绝 python 错误 - PermissionError: [Errno 13] Permission denied python error 有没有办法用 pytesseract 和 python 修复权限被拒绝的错误? - Is there a way to fix permission denied error with pytesseract and python? Python:安装pip时权限被拒绝错误 - Python: permission denied error when installing pip 处理 Windows Python 中的权限被拒绝错误 - Handle Permission Denied Error in windows Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM