Python| os.ftruncate() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.ftruncate()
方法用于将指定文件描述符对应的文件截断到指定长度。
该方法等价于os.truncate(fd, length)
方法。
Syntax: os.ftruncate(fd, length)
Parameters:
fd: The file descriptor representing the file to be truncated.
length: An integer value denoting length (in bytes) to which the file is to be truncated.
Return Type: This method does not return any value.
将以下文本视为名为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.ftruncate() method
# importing os module
import os
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR)
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
# Length (in Bytes) to which
# the file will be truncated
length = 72
# Truncate the file
# to at most given length
# using os.ftruncate() method
os.ftruncate(fd, length)
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
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.ftruncate() method
# importing os module
import os
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR)
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
# Length (in Bytes) to which
# the file will be truncated
length = 100
# Truncate the file
# to at most given length
# using os.ftruncate() method
os.ftruncate(fd, length)
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
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.ftruncate() method
# importing os module
import os
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR)
# Print the original size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
# specify the length as 0
# to delete the file content
length = 0
# Truncate the file
# to length 0
# using os.ftruncate() method
os.ftruncate(fd, length)
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
File size (in bytes): 100
Content of file Python_intro.txt:
File size (in bytes): 0