• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 在Python中操作文件之truncate()方法的使用教程

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

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python truncate,python中truncate,python f.truncate,truncate,truncate table等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

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

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

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

fileObject.truncate( [ size ])

</div>

参数

    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()

</div>

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

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

Read Line:

</div>

</div>

您可能想查找下面的文章:

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

相关文章

  • python插入数据到列表的方法
  • 在Python的web框架中配置app的教程
  • 分析Python的Django框架的运行方式及处理流程
  • 用Python实现协同过滤的教程
  • Python之父谈Python的未来形式
  • python使用opencv读取图片的实例
  • Python和perl实现批量对目录下电子书文件重命名的代码分享
  • Python中的闭包实例详解
  • python发送邮件示例(支持中文邮件标题)
  • python 网络编程常用代码段

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python天气预报采集器实现代码(网页爬虫)
    • python实现从ftp服务器下载文件的方法
    • Python实现统计单词出现的个数
    • Python采用raw_input读取输入值的方法
    • 浅谈python 四种数值类型(int,long,float,complex)
    • Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
    • Django实现自定义404,500页面教程
    • 浅谈五大Python Web框架
    • 使用PDB简单调试Python程序简明指南
    • python实现ftp客户端示例分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有