簡體   English   中英

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

[英]How do I open a text file in TextEdit from Python on Mac?

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

我正在編寫一個程序,它根據數據庫中的數據創建報表,將該數據寫入文本文件,然后啟動該文本文件以便打印。

我該如何實現這一目標?

我試圖使用webbrowser但沒有運氣。

您可以使用/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 os.system 模塊而不是os.system ,因為它更容易避免逃避它的問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM