Python node for getting Date and Time of files

Hello Dynamo users. I am pretty new in using Python and I I have been stuck with code problem. I try to create node which allows to define the Time of last modification of list of files. I can not process list of files but the node for one file works perfectly. Do you have any advices to solve it?Get%20time

Are there any exceptions or errors? You are using a for loop to access each item in numberList, but you are overwriting the file variable in each iteration.

Should I use file [ x ]= time…? I doesn’t work either.
Theerror is “expected an indented block”

The exact information is here. On the right side workable code on the left not.

import os, time

def getYYMMDD(filepath):	
    statbuf = os.stat(filepath)
    date = time.localtime((statbuf.st_mtime))
    # Debug
    #print str(date)

    # Extract out the year, month and day from the date
    year = date[0]
    month = date[1]
    day = date[2]
    # YY
    strYear = str(year)[2:]
    # MM
    if (month <=9):
        strMonth = '0' + str(month)
    else:
        strMonth = str(month)
    # DD
    if (day <=9):
        strDay = '0' + str(day)
    else:
        strDay = str(day)
    return (strYear+strMonth+strDay)

files = IN[0]    
newlist = []
for file in files:
    newlist.append(getYYMMDD(file))
OUT = newlist

EDIT: your input is typed in…

1 Like

Thank you for reply. But it seems that files array is not defined in the code.

can show me the error code?

yes i tested on my end. it works fine?

I tried to use it.

This is mine for one file workable. I need to upgrade it for list of files.

use the new edited code. with files = IN[0]

Thank you it works. The only question is what if I would like to get time as well?

def getYYMMDD(filepath):	
    epoch_datetime = os.path.getmtime(filepath)
    date_time = datetime.fromtimestamp(epoch_datetime)
    return date_time

use this function

I fix it. But wrote it in other way round. Here is the code.
But the thig is that I could not add “0” to numbers 9 and lower for hours, minutes, and seconds. Probably they are not integers for using (hour <=9)?

#HH
if (hour <=9):
strHour = ‘0’ + str(hour)
else:
strHour = str(hour)
This does not work
Said unexpected token ‘else’

Finally I wrote workable code
code%20for%20date%20time%20pic
Thanks for help and advices.

You can try that:

import clr
import sys
sys.path.append(“C:\Program Files (x86)\IronPython 2.7\Lib”)
import os
import datetime
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.

def modification_date(filename):
t = os.path.getctime(filename)
return datetime.datetime.fromtimestamp(t)

dataEnteringNode = IN[0]
dlist =
for d in dataEnteringNode:
try:
dlist.append(modification_date(d))
except:
dlist.append(“failed”)
#Assign your output to the OUT variable.
OUT = dlist

and/or that:

import clr
import sys
sys.path.append(“C:\Program Files (x86)\IronPython 2.7\Lib”)
import os
import datetime
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.

def modification_date(filename):
t = os.path.getmtime(filename)
return datetime.datetime.fromtimestamp(t)

dataEnteringNode = IN[0]
dlist =
for d in dataEnteringNode:
try:
dlist.append(modification_date(d))
except:
dlist.append(“failed”)
#Assign your output to the OUT variable.
OUT = dlist