什么是python truncate
从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。
python truncate语法
fileObject.truncate( [ size ])
python truncate参数
size,可选,如果存在则文件截断为 size 字节。
python truncate返回值
该方法没有返回值。
python 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 truncate函数用法,在裁剪文件方面经常会使用到,可以就这种方法先进行一个练习。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试