Opening Files with Default Application in Windows and Mac OS Using Python
In Python, the need often arises to open a document using its default application. This mimics the action of double-clicking a file icon in File Explorer or Finder. To achieve this, Python offers a robust solution.
Implementation:
To open a file with its default application, Python provides a powerful tool in the form of the subprocess module. The following code snippet demonstrates how to open a file using the default application on both Windows and Mac OS:
import subprocess, os, platform # Determine the operating system if platform.system() == 'Darwin': # macOS subprocess.call(('open', filepath)) elif platform.system() == 'Windows': # Windows os.startfile(filepath) else: # linux variants subprocess.call(('xdg-open', filepath))
In the code:
Additional Notes:
The above is the detailed content of How Can Python Open Files Using Their Default Applications on Windows and macOS?. For more information, please follow other related articles on the PHP Chinese website!