Chapters
Python114 chapters

ReferenceFile methods

file.write()

Writes a string to the file, adding no newline of its own.

Signature

f.write(text)

Parameters

NameMeans
textwhat to write

Returns

The number of characters written.

Example

Python
with open("notes.txt", "w", encoding="utf-8") as f:
    print(f.write("hello"))
    f.write("\nsecond\n")

with open("notes.txt", encoding="utf-8") as f:
    print(f.read().strip())

Output

5
hello
second

See also