Dimitra Micha
Dimitra Micha

Reputation: 2129

Open a file from a specific program from python

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

I am using a program called Blender and I want to write a script in python which open a .blend file but using the blender.app which is located in the same folder with the blend file, not with the blender.app which is located in Applications. (using Macosx)

So I was thinking that this should do the job...but instead it opens blender twice...

import os

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

I also tried this one

import os

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

but unfortunately it opens the .blend file with the default blender.app which is located in Applications...

any idea?

Upvotes: 3

Views: 4736

Answers (2)

Philipp
Philipp

Reputation: 49802

This cannot work since the system command gets executed in a subshell, and the chdir is only valid for that subshell. Replace the command by

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

Upvotes: 4

jdi
jdi

Reputation: 92559

Have you tried telling the open command 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 telling open to just open two different things. Not one with the other.

Upvotes: 1

Related Questions