Python で scikits.audiolab をインストールするときには注意が必要です。 コンパイルと実行の際にエラーの出る場合があります。
Continue reading Python: Caution on scikits.audiolab installationTag Archives: Python
python script for confirmation of sending email
Here is the script of python for confirm sending email, which was used when I was establishing mail server on Amazon AWS EC2, on python 2.7.9.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import smtplib from email.MIMEText import MIMEText from email.Utils import formatdate from_addr = 'sender@sender.com' to_addr = 'your_address@you.com' msg = MIMEText("test") msg['Subject'] = 'subject' msg['From'] = from_addr msg['To'] = to_addr msg['Date'] = formatdate() s = smtplib.SMTP() # Set server and port. Localhost and port 25 are set as default. # s.connect([host[, port]]) s.connect() # Login if the server restrict user of sending mail # s.login('user_name', 'password') s.sendmail(from_addr, [to_addr], msg.as_string()) s.close() |
Python で自分自身のファイル名を取得する
python のスクリプト(.py) を作って、スクリプト内で自分自身のファイル名を種痘する場合のお話です。
abc.py ファイル を実行した場合に、 abc.log のようなログファイルを作る際に使えます。
カレントディレクトリを起点にしたファイル名
|
1 |
__file__ |
ここからは、次のようにしてモジュールを読み込む必要があります。
|
1 |
from os import path |
ディレクトリを除いたファイル名
|
1 |
path.basename(__file__) |
ディレクトリに対してこれを実行すると、ディレクトリ名が取得できます。
ファイル名(拡張子なし)
|
1 |
path.splitext(path.basename(__file__))[0] |
“.” で文字列を区切ってリストにします。 “a.b.c” のように “.” が2つ以上ある場合は 最後の “.” で区切って (“a.b”, “c”) を返します。
path.basename(__file__)[0] をファイルの存在するディレクトリで実行した場合も同じ結果になります。 (crontab などで実行すると思わぬ結果になります。 “a/b.py” が (“a/b”, “.py”) になります。)




