Python os.ftruncate() Method
Example
Truncate a file to a specified length:
#Import os Library
import os
# open file
fd = os.open("test.txt", os.O_RDWR)
#Print original size of file (in bytes)
print("File size (in
bytes):", os.stat(fd).st_size)
length = 10
#Truncate file to 10
bytes
os.ftruncate(fd, length)
#Print new size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
#close file
os.close(fd)
Run Code »
Definition and Usage
The os.ftruncate() method truncates a file
to the specified length.
Note: Available on both WINDOWS and UNIX platform.
Syntax
os.ftruncate(fd, length)
Parameter Values
| Parameter | Description |
|---|---|
| fd | Required. Represents a file descriptor |
| length | Required. Specifies the size (in bytes) to which the file will be truncated |
Technical Details
| Return Value: | None |
|---|---|
| Python Version: | 2.6 |
| Change Log: | 3.5: Added support for Windows |