Python File truncate() Method Last Updated : 12 Dec, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisites: File Objects in Python Reading and Writing to files in Python Truncate() method truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. To truncate the file, you can open the file in append mode or write mode. Syntax: fileObject.truncate(size) Example: See the below image for file size. Let's change the file size to 100 bytes. Python3 1== # Python program to demonstrate # truncate() method fp = open('file1.txt', 'w') # Truncates the file to specified # size fp.truncate(100) # Closing files fp.close() Output: With statement In the above approaches, every time the file is opened it is needed to be closed explicitly. If one forgets to close the file, it may introduce several bugs in the code, i.e. many changes in files do not go into effect until the file is properly closed. To prevent this with statement can be used. with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner. There is no need to call file.close() when using with statement. The with statement itself ensures proper acquisition and release of resources. Let's change the above file to 50 bytes Python3 # Python program to demonstrate # truncate method using with statement with open('file1.txt', 'w') as fp: fp.truncate(50) Output: Note: To know more about with statement click here. Comment More infoAdvertise with us Next Article Python | os.truncate() method R rutujakawade24 Follow Improve Article Tags : Technical Scripter Python Practice Tags : python Similar Reads Python | os.truncate() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.truncate() method in Python is used to truncate the file indicated by the spec 4 min read Python | os.truncate() method os.truncate() method truncates the file corresponding to the path so that it is at the most length bytes in size. This function can support file descriptors too. In this article, we will learn about Python os.truncate() method. os.truncate() Method Syntax in PythonSyntax: os.truncate(path, length) P 3 min read Python - PyTorch trunc() method PyTorch torch.trunc() method returns a new tensor with the truncated integer values of the elements of input/ after removing the decimal portion of the number. Syntax: torch.trunc(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t 1 min read Python String lstrip() Method The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.Let's take an example to remove whitespace from the starting of a string.Pythons = " Hello Python!" res = s.lstrip() print(res)OutputHell 2 min read Python String center() Method center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want. Example:Pythons1 = "hello" s2 = s1.center(20) print(s2)Output hello Explanation: Here, the 2 min read Python String strip() Method strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.Let's take an example to remove whitespace from both ends of a string.Pythons = " GeeksforGeeks 2 min read Like