在Python中操作文件之truncate()方法的使用教程


Posted in Python onMay 25, 2015

 truncate()方法截断该文件的大小。如果可选的尺寸参数存在,该文件被截断(最多)的大小。

大小默认为当前位置。当前文件位置不改变。注意,如果一个指定的大小超过了文件的当前大小,其结果是依赖于平台。

注意:此方法不会在当文件工作在只读模式打开。
语法

以下是truncate()方法的语法:

fileObject.truncate( [ size ])

参数

    size -- 如果可选参数存在,文件被截断(最多)的大小。

返回值

此方法不返回任何值。
例子

下面的例子显示 truncate()方法的使用。

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

line = fo.readline()
print "Read Line: %s" % (line)

# Now truncate remaining file.
fo.truncate()

# Try to read file now
line = fo.readline()
print "Read Line: %s" % (line)

# Close opend file
fo.close()

当我们运行上面的程序,它会产生以下结果:

Name of the file: foo.txt
Read Line: This is 1st line

Read Line:
Python 相关文章推荐
python实现随机密码字典生成器示例
Apr 09 Python
使用Python生成随机密码的示例分享
Feb 18 Python
Python函数中的函数(闭包)用法实例
Mar 15 Python
python实现redis三种cas事务操作
Dec 19 Python
python实现从pdf文件中提取文本,并自动翻译的方法
Nov 28 Python
python使用递归的方式建立二叉树
Jul 03 Python
python实现中文文本分句的例子
Jul 15 Python
django使用admin站点上传图片的实例
Jul 28 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 Python
Python切片列表字符串如何实现切换
Aug 06 Python
python3让print输出不换行的方法
Aug 24 Python
Python利用pip安装tar.gz格式的离线资源包
Sep 14 Python
Python中tell()方法的使用详解
May 24 #Python
在Python中操作文件之seek()方法的使用教程
May 24 #Python
简单介绍Python中的readline()方法的使用
May 24 #Python
在Python中操作文件之read()方法的使用教程
May 24 #Python
在Python中使用next()方法操作文件的教程
May 24 #Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 #Python
讲解Python中fileno()方法的使用
May 24 #Python
You might like
让你的PHP同时支持GIF、png、JPEG
2006/10/09 PHP
PHP中的正规表达式(二)
2006/10/09 PHP
基于php验证码函数的使用示例
2013/05/03 PHP
php使用date和strtotime函数输出指定日期的方法
2014/11/14 PHP
PHP创建PowerPoint2007文档的方法
2015/12/10 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法
2016/10/12 PHP
PHP如何将图片文件上传到另外一台服务器上
2019/08/26 PHP
jQuery each()方法的使用方法
2010/03/18 Javascript
初学Jquery插件制作 在SageCRM的查询屏幕隐藏部分行的功能
2011/12/26 Javascript
javascript中的绑定与解绑函数应用示例
2013/06/24 Javascript
javascript中indexOf技术详解
2015/05/07 Javascript
给angular加上动画效遇到的问题总结
2016/02/17 Javascript
jQuery实现字符串全部替换的方法
2016/12/12 Javascript
JavaScript实现移动端轮播效果
2017/06/06 Javascript
Angular2.0实现modal对话框的方法示例
2018/02/18 Javascript
JS通用方法触发点击事件代码实例
2020/02/17 Javascript
解决echarts echarts数据动态更新和dataZoom被重置问题
2020/07/20 Javascript
[02:02:38]VG vs Mineski Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
深入理解NumPy简明教程---数组1
2016/12/17 Python
Python操作excel的方法总结(xlrd、xlwt、openpyxl)
2019/09/02 Python
python取均匀不重复的随机数方式
2019/11/27 Python
python pyenv多版本管理工具的使用
2019/12/23 Python
python通过对字典的排序,对json字段进行排序的实例
2020/02/27 Python
Python远程方法调用实现过程解析
2020/07/28 Python
基于Pytorch版yolov5的滑块验证码破解思路详解
2021/02/25 Python
俄罗斯在线水暖商店:Perfecto.ru
2019/10/25 全球购物
Hotels.com泰国:酒店预订网站
2019/11/20 全球购物
Lowe’s加拿大:家居装修、翻新和五金店
2019/12/06 全球购物
高级人员简历的自我评价分享
2013/11/03 职场文书
男方父母证婚词
2014/01/12 职场文书
初中体育教学反思
2014/01/14 职场文书
本科应届生求职信
2014/08/05 职场文书
个人违纪检讨书
2014/09/15 职场文书
公司食堂管理制度
2015/08/05 职场文书
MySql如何将查询的出来的字段进行转换
2022/06/14 MySQL