Select Page

How to Learn Python – The Ultimate Guide

Published: December 8, 2022

The read_txt function opens (open) and then reads (read) in one go, the file’s contents (fn) passed as a parameter. Below is how the function is invoked.


The read_txt_by_line function, on the other hand, opens (open) and then reads (read) the file’s contents (fn) passed as a parameter. This happens line-by-line, by first reading all the lines (readlines), and then looping through each line at a time (for line in lines). Here’s an example of that: 


The write_new_txt function, as its name implies, creates and opens (open) a new file (fn) in write-mode (w) using utf-8 encoding and writes a line (str) to the file:

write_new_txt(‘files_to_read/example.txt’, ‘this is a test…’)

The append_line_txt function, on the other hand, opens (open) an existing file (fn) in append-mode (a) using utf-8 encoding and adds a line (str) at the end of the file’s existing content:

append_line_txt(‘files_to_read/example.txt’, ‘this is an extra line’)

As you have seen, Python is highly versatile, can be used in typical business scenarios, and is great for automation.