Python File truncate() Metoda
Przykład
Otwórz plik z „a” do dołączenia, a następnie skróć plik do 20 bajtów:
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())
Definicja i użycie
Metoda truncate()
zmienia rozmiar pliku do podanej liczby bajtów.
Jeśli rozmiar nie zostanie określony, zostanie użyta bieżąca pozycja.
Składnia
file.truncate(size)
Wartości parametrów
Parameter | Description |
---|---|
size | Optional. The size of the file (in bytes) after the truncate. Default None, which means the current file stream position. |