Open a file from a specific program from python

I would like to do a very simple thing, but I am completely lost.

I am using a program called Blender and I want to write a script in python that opens the .blend file, but using blender.app, which is in the same folder with the blend file, not blender. an application that is in applications. (using macosx)

So I thought this should work ... but instead it opens the blender twice ...

import os

path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open blender.app Import_mhx.blend")

      

I tried this one too

import os

path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open Import_mhx.blend")

      

but unfortunately it opens a .blend file with blender.app by default which is in applications ...

any idea?

+3


source to share


2 answers


This may not work as the command system

runs in a subshell and chdir

is only valid for that subshell. Replace the command

os.system("open -a path/blender.app Import_mhx.blend")

      



or (much better)

subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),
                       "Import_mhx.blend"])

      

+3


source


Have you tried passing a command open

to open it with a specific application?

open -a /path/to/blender.app /path/to/Import_mhx.blend



Your first attempt was on the right track, but you were really talking about open

just discovering two different things. Not one with the other.

+1


source







All Articles