Python文件处理——文件的读写和计数

本文档介绍了Python中对文件的读写操作,包括创建文本文件并添加内容,以及如何读取文件。重点在于理解在写入文件时需要在每行末尾添加' '换行符,并在完成操作后记得关闭文件。此外,还提供了一个名为loadWords()的函数示例,用于读取words.txt文件,统计并输出单词数量,同时返回单词列表。
摘要由CSDN通过智能技术生成

一、 创建文本文件FarewellCambridge.txt。内容为:
Very quietly I take my leave
As quietly as I came here;
Quietly I wave good-bye
To the rosy clouds in the western sky.
The golden willows by the riverside
Are young brides in the setting sun;
Their reflections on the shimmering waves
Always linger in the depth of my heart.
The floating heart growing in the sludge
Sways leisurely under the water;
In the gentle waves of Cambridge
I would be a water plant!
That pool under the shade of elm trees
Holds not water but the rainbow from the sky;
Shattered to pieces among the duckweeds
Is the sediment of a rainbow-like dream?
To seek a dream? Just to pole a boat upstream
To where the green grass is more verdant;
Or to have the boat fully loaded with starlight
And sing aloud in the splendor of starlight.
But I cannot sing aloud
Quietness is my farewell music;
Even summer insects heap silence for me
Silent is Cambridge tonight!
并用写字板查看(注意:直接将上述诗句拷贝到程序中,不需要自己再次录入)。

源代码:

def wri_file():
    f=open("FarewellCambridge.txt",'w+')
    s='Very quietly I take my lea
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值