August 2018
Intermediate to advanced
248 pages
5h 51m
English
In a first example, let's imagine we want to write and update a file using two different programs (which could be services). Instead of creating two scripts, we will actually create a single script (in the retry_write_file.py file) that can be called by an argument to indicate what we want to do: create the file (first step) or update it.
We need a few imports, as follows:
import timeimport sysimport os
We define a function to create the file after a certain delay using time.sleep(after_delay), as follows:
def create_file(filename, after_delay=5): time.sleep(after_delay) with open(filename, 'w') as f: f.write('A file creation test')
Next, we define a function that helps append some text to the file, once it has been created, ...
Read now
Unlock full access