Python メモ。実行ファイル自身のファイル名を得る。
Python 2.6.5 学習メモ
些細なことでも記録していくよ。
こんな感じで取得していた。どひゃー はずかしい
(/home/hoge/Work/test.py)
#!/usr/bin/python #coding: utf-8 from sys import argv print argv[0]
実行結果
$ python test.py test.py
これだけで OK でした。
#!/usr/bin/python #coding: utf-8 print __file__
実行結果
$ python test.py test.py
弄る
#!/usr/bin/python #coding: utf-8 from os import path print u'絶対パス: ' + path.abspath(__file__) print u'ファイル名: ' + path.splitext(__file__)[0] print u'拡張子: ' + path.splitext(__file__)[1] import mimetypes print u'MIMEタイプ: ' + mimetypes.guess_type(__file__)[0]
実行結果
$ python test.py
絶対パス: /home/hoge/Work/test.py
ファイル名: test
拡張子: .py
MIMEタイプ: text/x-python