1. Text files
12days.txt
1.1. Reading files
f
is commonly used to refer to the file object from opening a file.filepath = "files/12days.txt"
f = open(filepath, "r")
print(f.read())
f.close()
1.1.1. Open file:
open()
function to open a file.- open(file, mode='r')
- Parameters:
file – a string for the file path to the file from the current directory.
mode – a string; “r” to read; “w” to write; “a” to append; “r+” to read and write; “b” for binary; “t” for text. Defaults are “rt” for read text.
Open file and return a corresponding file object.
- open(file, mode='r', encoding='utf-8')
- open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
- Parameters:
file – a string for the file path to the file from the current directory.
mode – a string; “r” to read; “w” to write; “a” to append; “r+” to read and write; “b” for binary; “t” for text. Defaults are “rt” for read text.
buffering – an optional integer used to set the buffering policy.
encoding – defaults to system; “utf-8” is recommended; see: https://docs.python.org/3/library/codecs.html#standard-encodings
errors – an optional string that specifies how encoding and decoding errors are to be handled.
newline – a string; can be None, ‘’, ‘n’, ‘r’, or ‘rn’
closefd – if a filename is given closefd must be True otherwise, an error will be raised.
opener – A custom opener can be used by passing a callable as opener.
Open file and return a corresponding file object.
1.1.2. Close file
close()
method to close the file object after opening it.Syntax:
- f.close()
1.1.3. Read method
read
method to read the whole of a small file.Syntax:
- fileobject.read(size)
- Parameters:
size – the number of characters (in text mode) or bytes (in binary mode) that are read; default 0 or omitted for whole file.
1.2. Context manager approach
filepath = "files/12days.txt"
with open(filepath, "r") as f:
f_contents = f.read()
print(f_contents)
1.3. Readlines
Syntax:
- f.readlines(size)
- Parameters:
size – optional; the number of characters or bytes returned exceed the size number, no more lines will be returned after that are returned.
filepath = "files/12days.txt"
with open(filepath, "r") as f:
f_contents = f.readlines()
print(f_contents)
f.readlines()
.['1 partridge in a pear tree\n', '2 turtle-doves\n', ...]
1.4. Iterating through the file
for line in f
to efficiently iterate over the lines of the file.filepath = "files/12days.txt"
with open(filepath, "r") as f:
for f_line in f:
print(f_line, end="")
1.5. Write to a file
w
as the mode to write to a file.filepath = "files/new_file.txt"
with open(filepath, "w") as f:
f.write("Test")
1.6. Appending to a text file
a
as the mode to append to the end of a file.filepath = "files/new_file.txt"
# overwrite file if it exists
with open(filepath, "w") as f:
f.write("Test 1")
# open again for appending
with open(filepath, "a") as f:
f.write("\nTest 2")
1.7. Copying parts of a text file
rfilepath = "files/12days.txt"
wfilepath = "files/12days_copy.txt"
with open(rfilepath, "r") as rf:
with open(wfilepath, "w") as wf:
for line in rf:
wf.write(line)
1 partridge in a pear tree
2 turtle-doves
3 French hens
...
step = 2
rfilepath = "files/12days.txt"
wfilepath = "files/12days_copy.txt"
with open(rfilepath, "r") as rf:
with open(wfilepath, "w") as wf:
for lineno, f_line in enumerate(rf):
if lineno % step == 0:
wf.write(f_line)
1 partridge in a pear tree
3 French hens
5 golden rings
...