File Methods
writelines()
Write a sequence of string lines to the file.
file.writelines(lines)
file.writelines() writes a sequence (list, tuple, generator) of strings to the file stream.
Parameters
| Name | Description |
|---|---|
| lines | An iterable of strings to write to the file. |
Returns
None.
Try it
Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as f:
lines = ['First line\n', 'Second line\n']
f.writelines(lines)
f.seek(0)
print(f.read().strip())Worth knowing
- No line separators added:
writelines()does not insert line separators; each item in the iterable must already include its own trailing newline (\n).