Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

Python File truncate() Method

The truncate() method in Python is used to resize the file to a specified size. If the specified size is less than the current size of the file, the file is truncated, and data beyond the specified size is lost. If the specified size is greater than the current size of the file, the file is extended, and the extended part contains null bytes.

Table of Contents

  1. Introduction
  2. truncate() Method Syntax
  3. Understanding truncate()
  4. Examples
    • Basic Usage
    • Truncating to a Smaller Size
    • Extending File Size
  5. Real-World Use Case
  6. Conclusion

Introduction

The truncate() method is a built-in file method in Python that allows you to resize a file to a specified size. This is useful for managing file sizes, especially when you need to limit the size of a file or remove unnecessary data.

truncate() Method Syntax

The syntax for the truncate() method is as follows:

file.truncate(size=None)

Parameters:

  • size (optional): The size to truncate the file to. If not specified, the current file position is used.

Returns:

  • None. The method truncates the file to the specified size.

Understanding truncate()

The truncate() method changes the size of the file to the specified number of bytes. If the specified size is smaller than the current size of the file, the file is truncated, and any data beyond the specified size is discarded. If the specified size is larger than the current size of the file, the file is extended with null bytes (\0).

Examples

Basic Usage

To demonstrate the basic usage of truncate(), we will create a file, write some data, and then truncate it to a smaller size.

Example

# Creating a sample file
with open("example.txt", "w") as file:
    file.write("Hello, world!\nWelcome to Python file handling.")

# Opening the file in read and write mode
file = open("example.txt", "r+")

# Truncating the file to 20 bytes
file.truncate(20)
file.seek(0)  # Move the pointer to the beginning of the file
print("File content after truncation:")
print(file.read())

# Closing the file
file.close()

Output:

File content after truncation:
Hello, world!
Wel

Truncating to a Smaller Size

This example shows how to truncate a file to a smaller size, removing data beyond the specified size.

Example

# Creating a sample file
with open("example.txt", "w") as file:
    file.write("This is a long line of text that will be truncated.")

# Opening the file in read and write mode
file = open("example.txt", "r+")

# Truncating the file to 10 bytes
file.truncate(10)
file.seek(0)  # Move the pointer to the beginning of the file
print("File content after truncation:")
print(file.read())

# Closing the file
file.close()

Output:

File content after truncation:
This is a

Extending File Size

This example shows how to extend a file size, filling the extended part with null bytes.

Example

# Creating a sample file
with open("example.txt", "w") as file:
    file.write("Short text.")

# Opening the file in read and write mode
file = open("example.txt", "r+")

# Extending the file to 20 bytes
file.truncate(20)
file.seek(0)  # Move the pointer to the beginning of the file
print("File content after extending:")
print(file.read())

# Closing the file
file.close()

Output:

File content after extending:
Short text.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top