如何创建包含python程序文件路径的字符串?

2024-06-25 22:32:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在创建一个python程序,它根据用户对程序的输入在Polyworks中执行特定的宏。现在的代码是:

roto.command.CommandExecute('MACRO EXEC("C:\\RotoWorks\\Macros\\CentrifugalCompressor")')

但是,这假设我们的程序总是安装在C:\RotoWorks中。理想情况下,我们的应用程序是可移植的。我确信有一种方法可以检索Rotoworks存储的文件路径,然后将其余的文件路径连接到最后。我该怎么做?你知道吗


Tags: 文件代码用户路径程序commandmacrosexec
1条回答
网友
1楼 · 发布于 2024-06-25 22:32:35

您可以从文件的__file__属性中检索路径。在该属性上使用os.path.abspath检索文件的绝对路径,然后使用os.path.dirname检索包含目录:

import os

file_directory = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(file_directory, other_path) # join directory to an inner path
roto.command.CommandExecute('MACRO EXEC({})'.format(path))

递归地使用os.path.dirname移出任意多个目录。你知道吗

相关问题 更多 >