2012年4月17日火曜日

開発環境

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

1.

コード(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)

入出力結果(Terminal)

$ python
Python 3.2.2 (default, Apr  4 2012, 12:40:34) 
[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.
>>> from mymod import *
>>> test()
file name: mymod.py
('行数:', 16, ' 文字数: ', 312)
>>> test()
file name: python_kamimura_blog
('行数:', 36, ' 文字数: ', 2484)
>>> quit()

0 コメント:

コメントを投稿