本文介绍了从python中的特定程序打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一件非常简单的事情,但我很迷茫.

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

我正在使用一个名为Blender的程序,我想在python中编写一个脚本以打开一个.blend文件,但要使用blender.app,该文件与blender文件位于同一文件夹中,而不是与blender.app位于应用程序"中. (使用Macosx)

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

我也尝试过这个

import os

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

但是不幸的是,它使用位于应用程序中的默认blender.app打开了.blend文件.

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

有什么主意吗?

推荐答案

由于system命令在子shell中执行,并且chdir仅对该子shell有效,因此此方法不起作用.将命令替换为

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

或(更好)

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

这篇关于从python中的特定程序打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 16:58