2009-01-14 64 views
3

重複:In Python, how do I get the path and name of the file that is currently executing?如何找出當前正在執行的腳本的路徑?的

我想找出路徑當前執行腳本。 我已經嘗試os.getcwd(),但只返回從腳本存儲的實際目錄運行腳本的目錄。

+0

謝謝Ray,我在詢問之前做過搜索,但從未發現過。 *聳肩* :) – Ashy 2009-01-14 20:27:31

+0

沒什麼大不了的;它也發生在我身上:http://stackoverflow.com/questions/168730/how-do-i-loop-through-all-files-in-a-folder-using-python-closed – 2009-01-14 20:55:08

回答

7

在Python中,__file__標識當前的Python文件。因此:

print "I'm inside Python file %s" % __file__ 

將打印當前的Python文件。請注意,這適用於導入的Python模塊以及腳本。

相關問題