[Tutor] getting the last file in a folder (reliably)

Pete O'Connell pedrooconnell at gmail.com
Mon Mar 21 22:58:42 CET 2011


Hi I have some code which works nine times out of ten. Maybe some could help
me make this a little more robust. I have a bunch of files in a folder with
a strict versioning based naming convention, where I want to open the
highest version number of a nuke script (not necessarily the one with the
newest modification date). So in the example folder contents listed below I
want to open "233_bb0072_comp_comp2k_v05.nk" (fourth from the bottom). Every
once in a while my python script ends up opening
233_bb0072_comp_comp2k_v04.nk (the next to last correct one). The script I
am using is shown below. Can anyone explain why it doesn't always work?


233_bb0072_comp_comp2k_v01.nk
233_bb0072_comp_comp2k_v01.nk~
233_bb0072_comp_comp2k_v02.nk
233_bb0072_comp_comp2k_v03.nk
233_bb0072_comp_comp2k_v03.nk~
233_bb0072_comp_comp2k_v04.nk
233_bb0072_comp_comp2k_v04.nk~
233_bb0072_comp_comp2k_v05.nk
233_bb0072_comp_comp2k_v05.autosave
233_bb0072_comp_comp2k_v05.nk~
233_bb0072_comp_comp2k_v06.nk

#######################################
import os
def openNewestCompCommandLine():

    theDirectory = "/path/to/my/comps/"
    theFilesInTheFolder = os.listdir(theDirectory)
    for aFile in theFilesInTheFolder:
        if "~" not in aFile:
            if "autosave" not in aFile:
                theNukeFileName = aFile

    theFullPath = theDirectory + theNukeFileName
    os.system("nuke "  + theFullPath)
if __name__ == '__main__':
    openNewestCompCommandLine()

for aFile in theFilesInTheFolder:
    print aFile
################################################
Pete
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110322/2ccc8cb6/attachment-0001.html>


More information about the Tutor mailing list