这里会显示出您选择的修订版和当前版本之间的差别。
| py-demo:readlines | py-demo:readlines 2008/02/01 14:14 当前版本 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ==== 遍历行并读行数 ==== | ||
| + | <code python> | ||
| + | filey=file('num.txt','rw'); | ||
| + | |||
| + | total=0; | ||
| + | while True: | ||
| + | |||
| + | lineValue=filey.readline(); | ||
| + | |||
| + | |||
| + | if len(lineValue)==0: | ||
| + | break; | ||
| + | lineValueF=float(lineValue); | ||
| + | total=lineValueF+total; | ||
| + | |||
| + | filey.seek(0) | ||
| + | LengthL=filey.readlines(); | ||
| + | length=len(LengthL); | ||
| + | |||
| + | print "The total is:", total; | ||
| + | print "The length is:",length; | ||
| + | filey.close(); | ||
| + | |||
| + | </code> | ||