The util Module

(Obsolete, Only in 1.5.2) The util module is included for backward-compatibility only. New code should use the replacement constructs shown in Examples 14-21 through 14-23.

Example 14-21 shows how remove(sequence, item) removes the given item, if found in the sequence.

Example 14-21. Emulating the util Module’s remove Function

File: util-example-1.py

def remove(sequence, item):
    if item in sequence:
        sequence.remove(item)

Example 14-22 shows how readfile(filename) => string reads the contents of a text file as a single string.

Example 14-22. Emulating the util Module’s readfile Function

File: util-example-2.py

def readfile(filename):
    file = open(filename, "r")
    return file.read()

Example 14-23 shows how readopenfile(file) => string returns the contents of an open file (or other file object).

Example 14-23. Emulating the util Module’s readopenfile Function

File: util-example-3.py

def readopenfile(file):
    return file.read()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.10