Metodo python3 scrivere il file ()
contorno
metodowrite () viene utilizzato per scrivere la stringa di file specificato.
Prima che il file è chiuso o prima che il buffer si svuota, il contenuto della stringa memorizzata nel buffer, allora non si può vedere nel documento è scritto contenuti.
grammatica
metodo write () ha la seguente sintassi:
fileObject.write( [ str ])
parametri
str - la stringa da scrivere nel file.
Valore di ritorno
Questo metodo non restituisce alcun valore.
Esempi
file di contenuto W3big.txt come segue:
1:www.w3big.com 2:www.w3big.com 3:www.w3big.com 4:www.w3big.com 5:www.w3big.com
L'esempio seguente dimostra il metodo write () di uso:
#!/usr/bin/python3 # 打开文件 fo = open("w3big.txt", "r+") print ("文件名: ", fo.name) str = "6:www.w3big.com" # 在文件末尾写入一行 fo.seek(0, 2) line = fo.write( str ) # 读取文件所有内容 fo.seek(0,0) for index in range(6): line = next(fo) print ("文件行号 %d - %s" % (index, line)) # 关闭文件 fo.close()
L'output sopra esempio è:
文件行号 0 - 1:www.w3big.com 文件行号 1 - 2:www.w3big.com 文件行号 2 - 3:www.w3big.com 文件行号 3 - 4:www.w3big.com 文件行号 4 - 5:www.w3big.com 文件行号 5 - 6:www.w3big.com
Controllare il contenuto dei file:
$ cat w3big.txt 1:www.w3big.com 2:www.w3big.com 3:www.w3big.com 4:www.w3big.com 5:www.w3big.com 6:www.w3big.com