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 回答