<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def printDuplicatedTPEDandTFAM(tped, tfam, samples, oldSamples, prefix):
 """Print the TPED and TFAM of the duplicated samples. :param tped: the ``tped`` containing duplicated samples. :param tfam: the ``tfam`` containing duplicated samples. :param samples: the updated position of the samples in the tped containing only duplicated samples. :param oldSamples: the original duplicated sample positions. :param prefix: the prefix of all the files. :type tped: :py:class:`numpy.array` :type tfam: :py:class:`numpy.array` :type samples: dict :type oldSamples: dict :type prefix: str The ``tped`` and ``tfam`` files are written in ``prefix.duplicated_samples.tped`` and ``prefix.duplicated_samples.tfam``, respectively. """

    # Print the TPED
    outputTPED = None
    try:
        outputTPED = open(prefix + ".duplicated_samples.tped", "w")
    except IOError:
        msg = "%(prefix)s.duplicated_samples.tped: can't write " \
              "file" % locals()
        raise ProgramError(msg)
    for row in tped:
        print >>outputTPED, "\t".join(row)
    outputTPED.close()

    # Print the TFAM
    nbSamples = len(tped[0][4:])
    newTFAM = [0 for i in xrange(nbSamples)]
    for samples, indexes in samples.iteritems():
        oldIndexes = oldSamples[samples]
        for i, index in enumerate(indexes):
            oldIndex = oldIndexes[i]
            newTFAM[index] = tfam[oldIndex]
    outputTFAM = None
    try:
        outputTFAM = open(prefix + ".duplicated_samples.tfam", "w")
    except IOError:
        msg = "%(prefix)s.duplicated_samples.tfam: can't write " \
              "file" % locals()
        raise ProgramError(msg)
    for row in newTFAM:
        print >>outputTFAM, "\t".join(row)
    outputTFAM.close()