Published 8月 15, 2018 by with 0 comment

習題16 - 讀寫文件




用Notepad++打下列的程式碼,
另存為ex16.py.
from sys import argv

script, filename = argv

print (f"We're going to erase {filename}.")
print ("If you don't want that, hit CTRL-C (^C).")
print ("If you do want that, hit RETURN")

input ("?")

print ("Opening the file...")
target = open (filename, 'w')

print ("Truncating the file. Goodbye!")
target.truncate()

print ("Now I'm going to ask you for three lines")

line1 = input ("line 1: ")
line2 = input ("line 2: ")
line3 = input ("line 3: ")

print ("I'm going to write these to the file.")

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print ("And finally, we close it.")
target.close()


然後用Windows的cmd, 執行python打開它. (並在後面加了1個參數: ex16_sample.txt)
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex16.py ex16_sample.txt
We're going to erase ex16_sample.txt.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines
line 1: 111AAA
line 2: 222BBB
line 3: 333CCC
I'm going to write these to the file.
And finally, we close it.

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


完成

此時會自行產生一個檔案ex16_sample.txt
內容就是我當時輸入的
C:\Users\Peter\Desktop\Python\LP3THW>type ex16_sample.txt
111AAA
222BBB
333CCC
C:\Users\Peter\Desktop\Python\LP3THW>


完成
這次的練習主要就是讀寫文件.
open: 開啟文件. 相關參數查詢如下
C:\Users\Peter\Desktop\Python\LP3THW>python -m pydoc open
open (filename,'w'): 不用w 無法寫入文件.
'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists
'b'       binary mode
't'       text mode (default)
'+'       open a disk file for updating (reading and writing)
'U'       universal newline mode (deprecated)
read: 讀取文件內容.
readline: 只讀取文件內容的某一行.
truncate: 清空文件. 小心小心.
write("ABC"): 將ABC寫入文件.
close: 關閉文件.

我們來修改一下程式碼.
Line 12 - 13, 打開文件看一下原本的內容.
Line 14, 重新打開一次文件, w是有清空文件的參數, 再加上utf-8的編碼.
Line 17, #將這行注釋起來不用了, 因為line14 有w參數(清空文件).
line 25, 將原本程式碼的line 25 - 30, 濃縮成一行完成.
from sys import argv

script, filename = argv

print (f"We're going to erase {filename}.")
print ("If you don't want that, hit CTRL-C (^C).")
print ("If you do want that, hit RETURN")

input ("?")

print ("Opening the file...")
target = open (filename)
print (target.read())
target = open (filename, 'w', encoding = "UTF-8")

print ("Truncating the file. Goodbye!")
#target.truncate()

print ("Now I'm going to ask you for three lines")

line1 = input ("line 1: ")
line2 = input ("line 2: ")
line3 = input ("line 3: ")

print ("I'm going to write these to the file.")

target.write(f"{line1}\n{line2}\n{line3}")

print ("And finally, we close it.")
target.close()

結果,
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex16.py ex16_sample.txt
We're going to erase ex16_sample.txt.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN
?
Opening the file...
111AAA
222BBB
333CCC
Truncating the file. Goodbye!
Now I'm going to ask you for three lines
line 1: 1a1a1a
line 2: 2b2b2b
line 3: 3c3c3c
I'm going to write these to the file.
And finally, we close it.

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


ex16_sample.txt文件也被修改了.
C:\Users\Peter\Desktop\Python\LP3THW>type ex16_sample.txt
1a1a1a
2b2b2b
3c3c3c
C:\Users\Peter\Desktop\Python\LP3THW>



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

0 comments:

張貼留言