1 min read

read a file by python

栾佳硕的任务:

1.建立一个文件,在文件中写入以下内容:

Watch the cartoon.

go fishing.

(不会了话看编程书)

f = open("datasets/lsj.txt","w")
f.writelines("Watch the  cartoon.\ngo fishing")
f.close()

2.把它读出来,并且用format方法显示出来。

with open("datasets/lsj.txt","r") as f:
    for line in f.readlines():
        print(line.strip())
## Watch the  cartoon.
## go fishing