Published 8月 22, 2018 by with 0 comment

習題20 - 函數與文件




先用Notepad++打下列的文件,
另存為ex20_sample.txt
This is Line 1
This is Line 2
This is Line 3

再用Notepad++打下列的程式碼,
另存為ex20.py. 我附上中文注釋方便好讀.
# -*- coding: UTF-8 -*-
#sys模組導入argv, 付與argv兩個變量script, input_file
from sys import argv
script, input_file = argv

#定義函數print_all, 讓它讀取文件內容
#並印出來
def print_all (f):
    print (f.read())

#定義函數 rewind, 讓讀取點回到文件開頭
def rewind (f):
    f.seek (0)

#定義函數peint_a_line, 給與兩個變量, 一個是line_count, 另一個是只讀取文件某一行.
#並印出來
def print_a_line (line_count, f):
    print (line_count, f.readline())

#定義current_file為打開argv第二變量所定義的文件名
current_file = open (input_file)

#執行函數print_all: 即印出來argv第二變量所定義的文件名
print ("First let's print the whole file:\n")
print_all (current_file)

#執行函數rewind: 讓讀取點回到argv第二變量所定義的文件名的文件開頭
print ("Now let's rewind, kind of like a type.")
rewind (current_file)


print ("Let's print three lines:")
#印出文件第一行內容
current_line = 1
print_a_line (current_line, current_file)

#印出文件第二行內容
current_line = current_line + 1
print_a_line (current_line, current_file)

##印出文件第三行內容
current_line = current_line + 1
print_a_line (current_line, current_file)

然後用Windows的cmd, 執行python打開它.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW

C:\Users\Peter\Desktop\Python\LP3THW>python .\ex20.py ex20_sample.txt
First let's print the whole file:

This is Line 1
This is Line 2
This is Line 3
Now let's rewind, kind of like a type.
Let's print three lines:
1 This is Line 1

2 This is Line 2

3 This is Line 3

C:\Users\Peter\Desktop\Python\LP3THW>

完成

然後我修改一下,
將Line 20: 修改一下, 印出文字, 因為readline自帶\n, 加上end="". 讓他不空行
# -*- coding: UTF-8 -*-
#sys模組導入argv, 付與argv兩個變量script, input_file
from sys import argv
script, input_file = argv

#定義函數print_all, 讓它讀取文件內容
#並印出來
def print_all (f):
    print (f.read())

#定義函數 rewind, 讓讀取點回到文件開頭
def rewind (f):
    f.seek (0)

#定義函數peint_a_line, 給與兩個變量, 一個是line_count, 另一個是只讀取文件某一行.
#並印出來
#修改一下, 印出文字
#因為readline自帶\n, 加上end="". 讓他不空行
def print_a_line (line_count, f):
    print ("Line", line_count, ": ", f.readline(), end="")

#定義current_file為打開argv第二變量所定義的文件名
current_file = open (input_file)

#執行函數print_all: 即印出來argv第二變量所定義的文件名
print ("First let's print the whole file:\n")
print_all (current_file)

#執行函數rewind: 讓讀取點回到argv第二變量所定義的文件名的文件開頭
print ("Now let's rewind, kind of like a type.")
rewind (current_file)


print ("Let's print three lines:")
#印出文件第一行內容
current_line = 1
print_a_line (current_line, current_file)

#印出文件第二行內容
current_line = current_line + 1
print_a_line (current_line, current_file)

##印出文件第三行內容
current_line = current_line + 1
print_a_line (current_line, current_file)

結果可以看到:
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex20-1.py ex20_sample.txt
First let's print the whole file:

This is Line 1
This is Line 2
This is Line 3
Now let's rewind, kind of like a type.
Let's print three lines:
Line 1 :  This is Line 1
Line 2 :  This is Line 2
Line 3 :  This is Line 3
C:\Users\Peter\Desktop\Python\LP3THW>

輸出結果不再空行了.
另外全局變量, 跟函數變量的名字, 最好不一要一樣. 會搞混.


最初發表 / 最後更新: 2018.08.22 / 2018.08.22

0 comments:

張貼留言