Ejemplo n.º 1
0
import pickle
import nesterstdout

new_man = []

try:
    with open('man_data.txt', 'rb') as man_file:
        new_man = pickle.load(man_file)
except IOError as err:
    print('File error ' + str(err))
except pickle.PickleError as perr:
    print('Pickling error ' + str(perr))

nesterstdout.print_lol(new_man)
Ejemplo n.º 2
0
            (role, line_spoken) = each_line.split(':', 1)
            # strip is used to strip the whitespaces in line_spoken
            line_spoken == line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('The datafile is missing')

try:
    with open('man_data.txt', 'w') as man_file:
        nesterstdout.print_lol(man, fh=man_file)
    with open('other_data.txt', 'w') as other_file:
        nesterstdout.print_lol(other, fh=other_file)
    # if there an I/O error occur to writing data at this place,
    # data may be corrupted.so we need a way regardless of whether an IOError
    # has occured
    # Make sure files are closed.
except IOError as err:
    print("File Error " + str(err))

"""
    when we have a situation where we need code that
    need to run no matter what error occur,
    add that code to your try statement's finally suite.
"""