【问题标题】:How do I open a text file in TextEdit from Python on Mac?如何在 Mac 上的 Python 中打开 TextEdit 中的文本文件?
【发布时间】:2014-03-18 09:29:15
【问题描述】:
else:
   tkMessageBox.showinfo('Report Created', 'Your report was sucessfully created')
   file = 'Student Report.txt'
   os.system('TextEdit'+file)

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

我如何做到这一点?

我曾尝试使用网络浏览器,但没有成功。

【问题讨论】:

    标签: python macos file operating-system


    【解决方案1】:

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

    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 module 而不是os.system,因为它更容易避免逃避问题:

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

    【讨论】:

    • +1,顺便说一句,最好提一下为什么问题中的代码不起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 2013-10-16
    • 1970-01-01
    • 2020-08-24
    • 2011-04-29
    • 1970-01-01
    • 2013-11-10
    • 2013-03-08
    相关资源
    最近更新 更多