开发者

How can python return the filename currently being processed under Windows

开发者 https://www.devze.com 2023-03-20 14:11 出处:网络
I am new to python, and just want to make a simple change.We are generating a template file via mako under Windows 7 and I want to change the comments at the start of the output file from:

I am new to python, and just want to make a simple change. We are generating a template file via mako under Windows 7 and I want to change the comments at the start of the output file from:

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

to

//////////////开发者_开发百科///////////////////////////////////////////////////
// This file is automatically generated by Mako from ${filename}
/////////////////////////////////////////////////////////////////

so, what is the real code to insert the actual filename being processed?


import os

filename = os.path.basename(__file__)
print ('this is generated by ' + filename)


For the name of the python script itself, use __file__:

$ echo "print __file__" > test.py                    
$ python test.py 
test.py
0

精彩评论

暂无评论...
验证码 换一张
取 消