file operate in python (open write read close )

本文介绍Python中文件操作的基础知识,包括如何使用`open()`函数打开文件、`read()`, `readline()`, `readlines()`用于读取文件内容、`write()`和`writelines()`用于写入文件、以及`close()`确保文件正确关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. file open:
    fd = open(N1,N2) :N1:path of the file to be open like '/home/warrior/Downloads/test.txt'
                                  N2:some  choiceable parameter
                                        'r':read only   'w':write only   'r+':read and write    'a':write and will add content at the end of file
2. file read:  
    content = fd.read():read all content of the file 
    oneline = fd.readline():read one line content of the file
    lastContent = fd.readlines():read the content from current  vernier to the end of the file
3. file write:
    fd.write(content):when opening in 'w' it will write content cover the original content
                                when opening in 'a' it will write content add at the end of the file but not start one new line again
    fd.writelines(content): when opening in 'w' it will write content cover the original content
                                        when opening in 'a' it will write content add at the end of the file and start one new line again
4. file close:
    fd.close():when open one file must close it as you will not use it .
5. file others:
    fd.seek(number):set the vernier of the file
    
















<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(25) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值