如何从Mac上的Python中的TextEdit中打开文本文件? [英] How do I open a text file in TextEdit from Python on Mac?

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

问题描述

else:
   tkMessageBox.showinfo('Report Created', 'Your report was sucessfully created')
   file = 'Student Report.txt'
   os.system('TextEdit'+file)

我正在编写一个程序,该程序可以根据数据库中的数据创建报表,然后将该数据写入文本文件,然后启动该文本文件以便可以打印.

I am writing a program that creates a report from data from a database, writes that data to a text file and then is supposed to launch that text file so that it can be printed.

我该如何实现?

我尝试使用网络浏览器,但是没有运气.

I have attempted to use webbrowser but no luck.

推荐答案

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

You can use the /usr/bin/open OSX utility:

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,因为避免转义问题更容易:

You should also use the subprocess module instead of os.system, as it is much easier to avoid escaping issues with it:

import subprocess
subprocess.call(['open', '-a', 'TextEdit', file])

这篇关于如何从Mac上的Python中的TextEdit中打开文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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