Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。
os.truncate()
Python中的方法用于将指定路径指示的文件截断为最大指定长度。
用法: os.truncate(path, length)
参数:
path:代表文件系统路径的path-like对象。这将指示文件被截断。
path-like对象是表示路径的字符串或字节对象。 length:一个整数值,表示文件将被截断到的长度(以字节为单位)。
返回类型:此方法不返回任何值。
将以下文本视为名为Python_intro.txt的文件的内容。
Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
# Python program to explain os.truncate() method
# importing os module
import os
# File path
path = "/home / ihritik / Desktop / Python_intro.txt"
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
# Length (in Bytes) to which
# the file will be trunctated
length = 72
# Truncate the file
# to at most given length
# using os.truncate() method
os.truncate(path, length)
# Print the content of file
print("Content of file Python_intro.txt:")
with open(path, 'r') as f:
print(f.read())
# Print the new size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
File size (in bytes): 409 Content of file Python_intro.txt: Python is a widely used general-purpose, high level programming language File size (in bytes): 72
将以下文本视为名为Python_intro.txt的文件的新内容。
Python is a widely used general-purpose, high level programming language
# Python program to explain os.truncate() method
# importing os module
import os
# File path
path = "/home / ihritik / Desktop / Python_intro.txt"
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
# Length (in Bytes) to which
# the file will be trunctated
length = 72
# Truncate the file
# to at most given length
# using os.truncate() method
os.truncate(path, length)
# Print the content of file
print("Content of file Python_intro.txt:")
with open(path, 'r') as f:
print(f.read())
# Print the new size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
File size (in bytes): 72 Content of file Python_intro.txt: Python is a widely used general-purpose, high level programming language File size (in bytes): 100
截断文件后的实际文件内容大小为72字节至100字节:
直到其原始大小的文件内容都没有改变,但是为了将文件大小增加到指定的大小,它填充了一些无效字符。
# Python program to explain os.truncate() method
# importing os module
import os
# File path
path = "/home / ihritik / Desktop / Python_intro.txt"
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
# specify the length as 0
# to delete the file content
length = 0
# Truncate the file
# to length 0
os.truncate(path, length)
# Print the content of file
print("Content of file Python_intro.txt:")
with open(path, 'r') as f:
print(f.read())
# Print the new size of the file (in bytes)
print("File size (in bytes):", os.path.getsize(path))
# Consider the same Python_intro.txt file
# used in above example for this example
File size (in bytes): 100 Content of file Python_intro.txt: File size (in bytes): 0
相关用法
- Python next()用法及代码示例
- Python os.dup()用法及代码示例
- Python set()用法及代码示例
- Python Decimal max()用法及代码示例
- Python PIL ImageOps.fit()用法及代码示例
- Python os.rmdir()用法及代码示例
- Python sympy.det()用法及代码示例
- Python Decimal min()用法及代码示例
- Python os.readlink()用法及代码示例
- Python os.writev()用法及代码示例
- Python os.readv()用法及代码示例
- Python PIL RankFilter()用法及代码示例
- Python os.rename()用法及代码示例
- Python os.sendfile()用法及代码示例
注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.truncate() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。