python2.4以上を想定しています。


正規表現

HTTP/HTTPS接続

文字コードの変換

文字列のエンコーディングを変換するには次のようにすればよい。(2.4)

str.encode("cp932")
str.decode("euc-jp")

UTF-8の内部コードを直接ソースファイルに書くには

u"あいうえお"

みたいに文字列の頭にuをつける。

r"あいうえお"

みたいにrをつけるとraw文字列(?)というのになるらしい。文字列のバイトをそのままという意味か?

ヘルプの引き方、モジュールのメンバの調べ方

help('modules') #利用可能なモジュールの一覧

コマンドライン引数の処理

引数はsys.argvにリスト形式で入っている。またオプション引数の解析はgetoptが使える。

getopt(args, shortopts, longopts=[])

getoptを使った引数の解析。

import sys, getopt
try:
    optlist, args = getopt.getopt(sys.argv[1:], "hs:", longopts=["help", "size="])
except getopt.GetoptError:
    #エラー処理
    sys.exit(0)

for opt, args in optlist:
    if opt in ("-h", "--help"):
        hogehoge
    if opt in ("-s", "--size"):
        fugofugo

ファイルIO

f = open('file_name', mode)

モード

ファイルオブジェクトから一行ごとに取り出すには

for line in fileobj:

ファイル操作

os.tmpfile()           一時ファイルのファイルオブジェクトを返す
os.chdir(path)         chdir
os.getcwd()            pwd
os.chmod(path,mode)    chmod
os.listdir(path)       pathに含まれるファイルとディレクトリのリスト
os.mkdir(path[,mode])  ディレクトリ作成
os.mkdirs(path[,mode]) 再帰的なディレクトリ作成
os.remove(path)        ファイル削除
os.removeddirs(path)   再帰的なディレクトリ削除
os.rename(src, dst)    改名
os.renames(old, new)   再帰的にパスを削除&作成する
os.rmdir(path)         ディレクトリ削除

glob.glob("*.exe")     ワイルドカードによるファイルのリスト

os.path.abspath(path)
os.path.basename(path)
os.path.dirname(path)
os.path.exists(path)
os.path.expanduser(path)       ~をユーザーのホームの置き換える
os.path.expandvars(path)       環境変数を展開する$name, ${name}
os.path.getatime(path)
os.path.getttime(path)
os.path.getctime(path)
os.path.getsize(path)
os.path.isabs(path)
os.path.isfile(path)
os.path.isdir(path)
os.path.islink(path)
os.path.ismount(path)
os.path.join(path[,path...])   パスの結合
os.path.normcase(path)
os.path.normpath(path)
os.path.realpath(path)
os.path.samefile(path1, path2)
os.path.sameopenfile(fp1, fp2)
os.path.split(path)            headとtailに分解
os.path.splitdrive(path)       drive, tailに分解
os.path.splittext(path)         root, extのペア

CategoryPython


Attach file: file_ssl.zip 549 download [Information]

|New|Edit|Freeze|Diff|History|Attach|Copy|Rename|
Last-modified: 2005-09-20 (Tue) 00:00:00
HTML convert time: 0.011 sec.