rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

PythonによるLinuxプログラミングインタフェース(3章 stderror)

 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 translate number to string
    """
    try:
        f = open(path, encoding='utf-8')
    except IOError as e:
        print("err: {0}".format(e.strerror))
    else:
        print("Success")


if __name__ == '__main__':
    path = "./nothing"
    my_open(path)
    path = "p52_strerror.py"
    my_open(path)