2012年4月19日木曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)のまとめ演習3(__name__属性)を解いてみる。

3.

コード(TextWrangler)

#!/usr/bin/env python
#encoding: utf-8

def countLines(name):
 file = open(name)
 lines = file.readlines()
 return len(lines)

def countChars(name):
 file = open(name)
 strings = file.read()
 return len(strings)

def test():
 name = input('file name: ')
 return {'行数:': countLines(name), ' 文字数: ':countChars(name)}

if __name__ == "__main__":
 print(test())

入出力結果(Terminal)

$ python mymod.py
file name: mymod.py
{'行数:': 19, ' 文字数: ': 357}
$ python mymod.py
file name: sample.py
{'行数:': 16, ' 文字数: ': 227}
$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
>>> name = 'mymod.py'
>>> mymod.countLines(name)
19
>>> mymod.countChars(name)
357
>>> mymod.test()
file name: mymod.py
{'行数:': 19, ' 文字数: ': 357}
>>> quit()
$

0 コメント:

コメントを投稿