1. 程式人生 > 其它 >學習python第n''''天——我在看笨辦法學python(讀寫檔案)

學習python第n''''天——我在看笨辦法學python(讀寫檔案)

這是所有的程式碼

from sys import argv

sceipt, filename = argv

print(f"We are 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 am going to ask for three lines.") line1 = input("line 1: ") line2 = input("line 2: ") line3 = input("line 3: ") print("I am 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()

這是執行結果

PS C:\Users\HH\lpthw> python ex5.py ex5_test.txt
We are going to erase ex5_test.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 am going to ask 
for three lines. line 1: Mary had a little lamb line 2: It is fleece was white as snow line 3: It was also tasty I am going to write these to the file. And finally, we close it. PS C:\Users\HH\lpthw>

這是我的輸出的文字