2014-02-18 2 views
0
else: 
    tkMessageBox.showinfo('Report Created', 'Your report was sucessfully created') 
    file = 'Student Report.txt' 
    os.system('TextEdit'+file) 

Я пишу программу, которая создает отчет из данных из базы данных, записывает данные в текстовый файл и затем должен запускать этот текстовый файл, чтобы он мог быть напечатан.Как открыть текстовый файл в TextEdit из Python на Mac?

Как это достичь?

Я попытался использовать веб-браузер, но не повезло.

ответ

2

Вы можете использовать утилиту /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]) 
+0

+1, Кстати, было бы лучше сказать, почему код в вопросе не работает. – falsetru

Смежные вопросы