Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 106069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:32:19+00:00 2026-05-11T01:32:19+00:00

I need to be able to open a document using its default application in

  • 0

I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double-click on the document icon in Explorer or Finder. What is the best way to do this in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:32:20+00:00Added an answer on May 11, 2026 at 1:32 am

    open and start are command-interpreter things for Mac OS/X and Windows respectively, to do this.

    To call them from Python, you can either use subprocess module or os.system().

    Here are considerations on which package to use:

    1. You can call them via os.system, which works, but…

      Escaping: os.system only works with filenames that don’t have any spaces or other shell metacharacters in the pathname (e.g. A:\abc\def\a.txt), or else these need to be escaped. There is shlex.quote for Unix-like systems, but nothing really standard for Windows. Maybe see also python, windows : parsing command lines with shlex

      • MacOS/X: os.system('open ' + shlex.quote(filename))
      • Windows: os.system('start ' + filename) where properly speaking filename should be escaped, too.
    2. You can also call them via subprocess module, but…

      For Python 2.7 and newer, simply use

      subprocess.check_call(['open', filename]) 

      In Python 3.5+ you can equivalently use the slightly more complex but also somewhat more versatile

      subprocess.run(['open', filename], check=True) 

      If you need to be compatible all the way back to Python 2.4, you can use subprocess.call() and implement your own error checking:

      try:     retcode = subprocess.call('open ' + filename, shell=True)     if retcode < 0:         print >>sys.stderr, 'Child was terminated by signal', -retcode     else:         print >>sys.stderr, 'Child returned', retcode except OSError, e:     print >>sys.stderr, 'Execution failed:', e 

      Now, what are the advantages of using subprocess?

      • Security: In theory, this is more secure, but in fact we’re needing to execute a command line one way or the other; in either environment, we need the environment and services to interpret, get paths, and so forth. In neither case are we executing arbitrary text, so it doesn’t have an inherent ‘but you can type 'filename ; rm -rf /'‘ problem, and if the file name can be corrupted, using subprocess.call gives us little additional protection.
      • Error handling: It doesn’t actually give us any more error detection, we’re still depending on the retcode in either case; but the behavior to explicitly raise an exception in the case of an error will certainly help you notice if there is a failure (though in some scenarios, a traceback might not at all be more helpful than simply ignoring the error).
      • Spawns a (non-blocking) subprocess: We don’t need to wait for the child process, since we’re by problem statement starting a separate process.

      To the objection ‘But subprocess is preferred.’ However, os.system() is not deprecated, and it’s in some sense the simplest tool for this particular job. Conclusion: using os.system() is therefore also a correct answer.

      A marked disadvantage is that the Windows start command requires you to pass in shell=True which negates most of the benefits of using subprocess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 187k
  • Answers 187k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all new is a reserved word. You need… May 12, 2026 at 5:26 pm
  • Editorial Team
    Editorial Team added an answer There are two concepts here. Reference types vs. value types… May 12, 2026 at 5:26 pm
  • Editorial Team
    Editorial Team added an answer I'm unsure what you're trying to accomplish with the pushing… May 12, 2026 at 5:26 pm

Related Questions

I'm developing an application targeting .NET Framework 2.0 using C# for which I need
I am on the verge of remaking an application for e-learning. But now I
I have an existing PDF and I can use FdFWriter to input to text
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.