[Tutor] getting the last file in a folder (reliably)
Rafael Durán Castañeda
rafadurancastaneda at gmail.com
Mon Mar 21 23:20:29 CET 2011
You are taking the last file from os.listdir that hasn't neither ~ nor
aoutosave, but os.listdir doesn't return a sorted list, so that file is
random. You must order or compare in the for loop.
2011/3/21 Pete O'Connell <pedrooconnell at gmail.com>
> 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
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110321/261ef5ba/attachment.html>
More information about the Tutor
mailing list