2013-07-02 18 views
5

私はいつも、__file__という変数は常にあなたに現在のファイル名を与えたと仮定しましたが、それはまさにそれがそうでないようです。私がこれが真実であると仮定すると、私はバグに直面していたからです。Pythonの__file__は実際に何をしていますか?

誰かが 「を検索すること__file__が最後のモジュールを指し、」これは、より正確であるようだが、私は本当に行うことになっているもの__file__知りたい私に言いました。

Pythonのドキュメントに記載されている具体的な内容が見つかりませんでした。 多くの場所はそれに言及しているようですが、それについてはあまり明確ではありません。ここから

http://docs.python.org/2/c-api/import.html?highlight=__file__

http://docs.python.org/2/c-api/module.html?highlight=__file__

+8

:http://stackoverflow.com/questions/7116889/python-file-attribute-absolute-or -relative –

+3

*「__file__」は最後に検索されたモジュールを指しています。「誰かが間違っていた、恐れています。 –

+0

@MartijnPieters Ha、まあ私はちょうど私が '__file__ – ffledgling

答えて

3
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present 
for C modules that are statically linked into the interpreter; for 
extension modules loaded dynamically from a shared library, it is the 
pathname of the shared library file. 

:これは役立つかもしれない http://mail.python.org/pipermail/python-dev/2010-February/097461.html

関連する問題