亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 python dm-script 中獲取當前文件路徑

如何在 python dm-script 中獲取當前文件路徑

POPMUISE 2023-06-06 15:06:29
我想獲取當前文件在數碼顯微圖像中的文件路徑python。我該怎么做呢?我試著用__file__但我明白了NameError: Name not found globally.我嘗試將 與以下代碼一起使用dm-script GetCurrentScriptSourceFilePath()來獲取 python 的值import DigitalMicrograph as DMimport time    # get the __file__ by executing dm-scripts GetCurrentScriptSourceFilePath()# function, then save the value in the persistent tags and delete the key# againtag = "__python__file__{}".format(round(time.time() * 100))DM.ExecuteScriptString(    "String __file__;\n" +     "GetCurrentScriptSourceFilePath(__file__);\n" +     "number i = GetPersistentTagGroup().TagGroupCreateNewLabeledTag(\"" + tag + "\");\n" +     "GetPersistentTagGroup().TagGroupSetIndexedTagAsString(i, __file__);\n");_, __file__ = DM.GetPersistentTagGroup().GetTagAsString(tag);DM.ExecuteScriptString("GetPersistentTagGroup().TagGroupDeleteTagWithLabel(\"" + tag + "\");")但似乎該GetCurrentScriptSourceFilePath()函數不包含路徑(這是有道理的,因為它是從字符串執行的)。我發現這篇文章推薦import inspectsrc_file_path = inspect.getfile(lambda: None)但這顯然src_file_path是"<string>"錯誤的。我試圖引發異常,然后使用以下代碼獲取它的文件名try:    raise Exception("No error")except Exception as e:    exc_type, exc_obj, exc_tb = sys.exc_info()    filename = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]    print("File: ", filename)但我又一次得到<string>了filename。我試圖從腳本窗口中獲取路徑,但找不到任何函數來獲取它。但是腳本窗口必須知道它的路徑在哪里,否則Ctrl+S不能工作。一些背景我正在開發用于數碼顯微照片的模塊。我也有測試文件。但是要在測試文件中導入(仍在開發的)模塊,我需要相對于測試文件的路徑。稍后模塊將安裝在某個地方,所以這應該不是問題。但是為了提供一組完整的測試,我希望能夠在不必安裝(不工作的)模塊的情況下執行測試。
查看完整描述

3 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

對于當前工作目錄的文件路徑,請使用:

import os
os.getcwd()


查看完整回答
反對 回復 2023-06-06
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

在您可以從 Python 腳本調用的 DM 腳本中,該命令GetApplicationDirectory()為您提供所需內容。通常,您會想要使用“open_save”,即

GetApplicationDirectory("open_save",0)

這將返回在使用文件/打開或在新圖像上使用文件/保存時出現的目錄(字符串變量)。但是,它不是用于“文件/保存工作區”或其他保存的內容。

從有關該命令的 F1 幫助文檔中:

http://img1.sycdn.imooc.com//647edb2400013b2b16830972.jpg

請注意,“當前”目錄的概念在 Win10 上與應用程序(包括 GMS)有些沖突。特別是“SetApplicationDirectory”命令并不總是按預期工作......


如果您想找出當前顯示的特定文檔的文件夾(圖像或文本),您可以使用以下 DM 腳本。這里的假設是,窗口是最前面的窗口。


documentwindow win = GetDocumentWindow(0)

if ( win.WindowIsvalid() )

    if ( win.WindowIsLinkedToFile() )

        Result("\n" + win.WindowGetCurrentFile())


查看完整回答
反對 回復 2023-06-06
?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

對于也需要這個(并且只想復制一些代碼)的每個人,我根據@BmyGuests 的回答創建了以下代碼。這會獲取腳本窗口綁定到的文件并將其保存為持久標記。然后從 python 文件中讀取此標記(并刪除標記)。


重要說明:這僅在您按下“執行腳本”按鈕的 python 腳本窗口中有效,并且僅當此文件已保存時!但是從那里開始,您可能正在導入應該提供該module.__file__屬性的腳本。這意味著這不適用于插件/庫。


import DigitalMicrograph as DM


# the name of the tag is used, this is deleted so it shouldn't matter anyway

file_tag_name = "__python__file__"

# the dm-script to execute, double curly brackets are used because of the 

# python format function

script = ("\n".join((

    "DocumentWindow win = GetDocumentWindow(0);",

    "if(win.WindowIsvalid()){{",

        "if(win.WindowIsLinkedToFile()){{",

            "TagGroup tg = GetPersistentTagGroup();",

            "if(!tg.TagGroupDoesTagExist(\"{tag_name}\")){{",

                "number index = tg.TagGroupCreateNewLabeledTag(\"{tag_name}\");",

                "tg.TagGroupSetIndexedTagAsString(index, win.WindowGetCurrentFile());",

            "}}",

            "else{{",

                "tg.TagGroupSetTagAsString(\"{tag_name}\", win.WindowGetCurrentFile());",

            "}}",

        "}}",

    "}}"

))).format(tag_name=file_tag_name)


# execute the dm script

DM.ExecuteScriptString(script)


# read from the global tags to get the value to the python script

global_tags = DM.GetPersistentTagGroup()

if global_tags.IsValid():

    s, __file__ = global_tags.GetTagAsString(file_tag_name);

    if s:

        # delete the created tag again

        DM.ExecuteScriptString(

            "GetPersistentTagGroup()." + 

            "TagGroupDeleteTagWithLabel(\"{}\");".format(file_tag_name)

        )

    else:

        del __file__


try:

    __file__

except NameError:

    # set a default if the __file__ could not be received

    __file__ = ""

    

print(__file__);


查看完整回答
反對 回復 2023-06-06
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號