2012年8月6日月曜日

開発環境

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

2.

コード(TextWrangler)

sample.py

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

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

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

def test():
 print("ファイル名を入力: ",end="")
 name = input()
 result = ("行数: " + str(countLines(name)) +  
   ", 文字数: " + str(countChars(name)))
 return result

入出力結果(Terminal)

$ 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.
>>> from sample import countLines
>>> name = 'sample.py'
>>> countLines(name)
17
>>> from sample import countChars
>>> countChars(name)
333
>>> from sample import test
>>> test()
ファイル名を入力: sample.py
'行数: 17, 文字数: 333'
>>> quit()
$ 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.
>>> from sample import *
>>> name="sample.py"
>>> countLines(name)
17
>>> countChars(name)
333
>>> test()
ファイル名を入力: sample.py
'行数: 17, 文字数: 333'
>>> quit()
$

0 コメント:

コメントを投稿