Python Bestand truncate() Methode
Voorbeeld
Open het bestand met "a" om toe te voegen en kap het bestand af tot 20 bytes:
f = open("demofile2.txt", "a")
f.truncate(20)
f.close()
#open
and read the file after the truncate:
f = open("demofile2.txt", "r")
print(f.read())
Definitie en gebruik
De truncate()
methode verkleint het bestand tot het opgegeven aantal bytes.
Als de maat niet is opgegeven, wordt de huidige positie gebruikt.
Syntaxis
file.truncate(size)
Parameterwaarden
Parameter | Description |
---|---|
size | Optional. The size of the file (in bytes) after the truncate. Default None, which means the current file stream position. |