Change language

Python File truncate () Method

|

Prerequisites :

The Truncate () method truncates the file size. If the optional size argument is specified, the file is truncated to (maximum) that size. The default size corresponds to the current position. The current file position does not change. Note that if the specified size exceeds the current file size, the result is platform-dependent: the options are: the file can remain unchanged, grow to the specified size as if it were filled with zeros, or grow to the specified size with undefined new content. 
To truncate a file, you can open the file in append or write mode.

Syntax :

 fileObject.truncate (size) 

Example:
See image below for file size.

Let’s change the file size to 100 bytes.

Exit :

With statement

In the above approaches, every time a file is opened, it must be closed explicitly. If you forget to close the file, it can lead to several errors in your code, i.e. many changes to files do not take effect until the file is properly closed. You can use the with assertion to prevent this. Python’s with statement is used to handle exceptions to make your code cleaner and much more readable. This makes it easier to manage shared resources such as file streams. Notice the following code example for how using the with statement makes your code cleaner. There is no need to call file.close () when used with assertion. The with statement itself ensures that resources are acquired and released correctly.

Let’s change the above file to 50 bytes

# Python program for demonstration
# truncate () method

 

fp = open ( ’ file1.txt’ , ’w’ )

 
# Truncates the file to uk given
# size

fp.truncate ( 100 )

 
# Closing files
fp.close ()

# Demo Python program
# truncation method used with operator

  

with open ( ’file1.txt’ , ’ w’ ) as fp:

fp.truncate ( 50 )

Output:

Note Note: to learn more about the statement,

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically