Python| os.truncate() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.truncate()
方法用于将指定路径所指示的文件截断到最多指定长度。
Syntax: os.truncate(path, length)
Parameters:
path: A path-like object representing a file system path. This will indicate the file to be truncated.
A path-like object is either a string or bytes object representing a path.
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.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 truncated
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 truncated
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