2014-02-01から1ヶ月間の記事一覧
第5章のP115のテンポラリファイル作成のpythonコード。 #/usr/bin/env #-*- encoding: utf-8 -*- import tempfile import os import time # don't use # os.tmpnam (see: http://docs.python.org/2/library/os.html#os.tmpnam) # os.tempnam (see: http://do…
第5章P109のtruncateについて。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): path = "./p109_truncate.txt" file = open(path, 'w', encoding='utf-8') file.write("abc") # write "abc" and offset move to 3 is end of file. print…
第5章のP105のpreadのサンプル。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): file = open(__file__, encoding='utf-8') orig = file.tell() print("before offset: {0}".format(file.tell())) offset = 10 file.seek(offset) lines …
5章P103のdupのpythonコード。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): def run(): py_dup() py_dup2() def py_dup(): newfd = os.dup(1) print("newfd: " + str(newfd)) os.close(2) newfd = os.dup(1) print("newfd: " + str(n…
4章のP90ページのread, write, lseekのプログラム。 #/usr/bin/env #-*- encoding: utf-8 -*- import sys def pythonic(): try: file = open(sys.argv[1], 'r+', encoding='utf-8') except IOError as e: print("err: " + e) else: try: ap = 2 argc = len(s…
4章P75のファイルをopenしてread、readした内容をwriteしてファイルをコピーするというプログラム。 pythonで書くと下記。 #/usr/bin/env #-*- encoding: utf-8 -*- import sys def usage_error(filename): print("{0} old-file new-file".format(filename)…
3章のstderrorに対するコード。 pythonだとエラーをキャッチしてから、e.strerrorで文字列としてエラーメッセージを取得できる。 #include <string.h> char *strerror(int errnum); #/usr/bin/env #-*- encoding: utf-8 -*- def my_open(path): """ os.strerror() is </string.h>…