[PYTHON] - Get last modified file from folder


#1

Hello guys,
I’ve been recently learning Python and started my practice by rewriting my MEL scripts.
One of them was a backup file script which queried the last modified file and saved it in a desirable folder.

For now I’m stuck with something probably very easy but got annoyed after
some hours and seeking for help :slight_smile: Could you please tell me how to store float data
back into a list and get the max number assigned to the file? Now I’m only getting
the last position from the array or getting and error when using the max() function.

I’ve checked this topic but found no solution.

import maya.cmds as cmds
import os

scriptPath = cmds.internalVar(usd=True)
fileList = cmds.getFileList(fld=scriptPath)

def getTimeModif (filesFolder, fileList):
timelist = []
for file in fileList:
timeList = os.path.getmtime(filesFolder + file)
return timeList

test = getTimeModif (scriptPath, myFiles)
print test


#2

I’ve managed to solve the problem :slight_smile:

import maya.cmds as cmds
import os

def getNewestFile (path, fileList):
timeList =[]

for file in fileList:
	getTime = os.path.getmtime(path + file)
	file = getTime
	timeList.append(file)

hiVal = timeList.index(max(timeList))
return fileList[hiVal]

scriptPath = cmds.internalVar(usd=True)
scriptList = os.listdir(scriptPath)

newestFile = getNewestFile(scriptPath, scriptList)
print newestFile