[英]How do I create a string containing the filepath of my python program?
We are creating a python program that executes specific macros within Polyworks based on user input into the program. 我们正在创建一个python程序,该程序将根据程序中的用户输入在Polyworks中执行特定的宏。 Right now the code is:
现在的代码是:
roto.command.CommandExecute('MACRO EXEC("C:\\RotoWorks\\Macros\\CentrifugalCompressor")')
However this assumes that our program is always installed in C:\\RotoWorks. 但是,这假定我们的程序始终安装在C:\\ RotoWorks中。 Ideally, our app is portable.
理想情况下,我们的应用程序是便携式的。 I'm sure theres a way to retrieve the filepath that Rotoworks is stored in, then just concatenate the rest of the filepath to the end.
我确信有一种方法可以检索Rotoworks存储在其中的文件路径,然后将其余的文件路径连接到末尾。 How do I do this?
我该怎么做呢?
You can retrieve the path from the __file__
attribute of the file. 您可以从文件的
__file__
属性中检索路径。 Use os.path.abspath
on that attribute to retrieve the absolute path of the file and then os.path.dirname
to retrieve the containing directory: 在该属性上使用
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))
Use os.path.dirname
recursively to move out as many directories as you want. 递归使用
os.path.dirname
移出所需的目录数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.