学习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>

这是我的输出的文本

 

 

 

 

posted on 2021-12-02 18:39  何奈时  阅读(106)  评论(0)    收藏  举报

导航