Writing Text Files
- Getting a File object
- Write Mothod
- Write Mothod Example
- Writelines Method
- Writelines Example
Getting a File object
- Get a file object with:
- a filename of filepath
- write mode (‘w’)
with open(filename, 'w') as f:
Write Method
Syntax
f.write(s)
- passing/write a string s to f
Write Mothod Example
with open('out.txt', 'w') as f:
f.write("Hello, world!")
f.write("Good morning!\n")
f.write("Good night!\n")
Writelines Method
Syntax
f.writelines(sequence)
- is the counterpart to readlines.
- write each item in a sequence as a string to f
- it accepts a sequence and write each item as a string to a file object
Writelines Example
- we want to wrote 5 lines to a file called out.txt
with open('out.txt', 'w') as f:
lines = [f'line{n}' for n in range(1,6)]
# list comprehension to generate a list with string items: each item will be the string line 1 to line 5
f.writelines(lines)
# there is no new line character, therefore all in one line
with open('out.txt', 'w') as f:
lines = [f'line{n}\n' for n in range(1,6)]
f.writelines(lines)
Strings | Variables | Lists | Tuples | Dictionary | ||||||
Control | Function | Files | Exceptions | |||||||
OOP | Algorithm | Data Structure | back |