python tempstr函数(python truncate是什么)
导读:1、说明...
1 、说明
从文件的首行首字符开始截断 ,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除 。
2 、语法
fileObject.truncate([size])3 、参数
size ,可选 ,如果存在则文件截断为 size 字节 。
4 、返回值
该方法没有返回值 。
5 、实例
#!/usr/bin/python #Openafile fo=open("foo.txt","rw+") print"Nameofthefile:",fo.name #Assumingfilehasfollowing5lines #Thisis1stline #Thisis2ndline #Thisis3rdline #Thisis4thline #Thisis5thline line=fo.readline() print"ReadLine:%s"%(line) #Nowtruncateremainingfile. fo.truncate() #Trytoreadfilenow line=fo.readline() print"ReadLine:%s"%(line) #Closeopendfile fo.close()当我们运行上面的程序 ,它会产生以下结果:
Nameofthefile:foo.txt ReadLine:Thisis1stline ReadLine:以上就是python truncate方法的介绍 ,在裁剪文件方面经常会使用到 ,可以就这种方法先进行一个练习 。更多Python学习指路:Python基础教程
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!