Open a file from a specific program from python

懵懂的女人 提交于 2019-12-21 20:02:25

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/9847696/open-a-file-from-a-specific-program-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!