![](/img/trans.png)
[英]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.