2

私はPythonを初めて使用し、簡単な変更を加えたいだけです。Windows 7でmakoを介してテンプレートファイルを生成していますが、出力ファイルの先頭にあるコメントを次のように変更したいと思います。

/////////////////////////////////////////////////////////////////
// This file is automatically generated by Mako
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// This file is automatically generated by Mako from ${filename}
/////////////////////////////////////////////////////////////////

それで、処理されている実際のファイル名を挿入するための実際のコードは何ですか?

4

2 に答える 2

4
import os

filename = os.path.basename(__file__)
print ('this is generated by ' + filename)
于 2011-07-13T17:02:51.420 に答える
1

Python スクリプト自体の名前には、次を使用します__file__

$ echo "print __file__" > test.py                    
$ python test.py 
test.py
于 2011-07-13T17:05:12.763 に答える