Skip to content Skip to sidebar Skip to footer

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) I am writing a program that cr

Solution 1:

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

NAMEopen--open files and directories

SYNOPSISopen [-e] [-t] [-f] [-F] [-W] [-R] [-n] [-g] [-h] [-b bundle_identifier] [-a application] file ... [--args arg1 ...]

DESCRIPTIONTheopen command opens a file (or a directory or URL), just asif you had double-clicked the file's icon. If no application name is specified, the default application as deter-
     mined via LaunchServicesis used to open the specified files.

     If the file isin 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 asif you had launched the application directly through its full path.  This behavior was also present inTiger.

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])

Post a Comment for "How Do I Open A Text File In Textedit From Python On Mac?"