您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Mac上的Python中从TextEdit中打开文本文件?

如何在Mac上的Python中从TextEdit中打开文本文件?

您可以使用/usr/bin/openOSX实用程序:

NAME
     open -- open files and directories

SYNOPSIS
     open [-e] [-t] [-f] [-F] [-W] [-R] [-n] [-g] [-h] [-b bundle_identifier] [-a application] file ... [--args arg1 ...]

DESCRIPTION
     The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as deter-
     mined via LaunchServices is used to open the specified files.

     If the file is in the form of a URL, the file will be opened as a URL.

     You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. For example, the following com-
     mand would open all Word files in the current working directory:

     open *.doc

     Opened applications inherit environment variables just as if you had launched the application directly through its full path.  This behavior was also present in Tiger.

您还应该使用subprocess模块而不是os.system,因为避免转义问题更加容易:

import subprocess
subprocess.call(['open', '-a', 'TextEdit', file])
python 2022/1/1 18:46:18 有331人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶