Metoda truncate() souboru Pythonu
Příklad
Otevřete soubor s "a" pro připojení a poté zkraťte soubor na 20 bajtů:
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())
Definice a použití
Metoda truncate()
změní velikost souboru na daný počet bajtů.
Pokud není velikost uvedena, použije se aktuální pozice.
Syntax
file.truncate(size)
Hodnoty parametrů
Parameter | Description |
---|---|
size | Optional. The size of the file (in bytes) after the truncate. Default None, which means the current file stream position. |