How do I open a text file in TextEdit from Python on Mac?(如何在 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 中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Mac 上的 Python 中打开 TextEdit 中的文本文件


基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01