[英]How do I insert '*.txt ' to a string which is a filepath in python?
[英]How do I create a string containing the filepath of my python program?
我们正在创建一个python程序,该程序将根据程序中的用户输入在Polyworks中执行特定的宏。 现在的代码是:
roto.command.CommandExecute('MACRO EXEC("C:\\RotoWorks\\Macros\\CentrifugalCompressor")')
但是,这假定我们的程序始终安装在C:\\ RotoWorks中。 理想情况下,我们的应用程序是便携式的。 我确信有一种方法可以检索Rotoworks存储在其中的文件路径,然后将其余的文件路径连接到末尾。 我该怎么做呢?
您可以从文件的__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
移出所需的目录数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.