Hello all,
I would like to extract the timestamp value of each folder’s name shown below and then identify the folder with the most recent timestamp. In the example below I would like to be able to select the path of the folder with timestamp 095900 (9:59 am)
This is the script I wrote and it will return the max timestamp out of all folder names, now how can I select the folder with this value?
import os
input_path = "C:\\Images\\ANGLE\\2021\\12\\20\\"
fileList = os.listdir(input_path)
value=[]
for i in range(len(fileList)):
list= fileList[i][30:36]
value.append(list)
max_value=max(value)
system.perspective.print(max_value)
Don’t use keywords used by Jython for variable names ie list
,dict
etc. Take each file name and split on _
char then get last element of the split which is the time value.
import os
input_path = "C:\\Images\\ANGLE\\2021\\12\\20\\"
fileList = os.listdir(input_path)
tstamps = [int(x.split('_')[-1]) for x in fileList]
print max(tstamps)
1 Like
Clever solution @dkhayes117. Thank you! How then I will be able to get file names of the folder with the most recent timestamp? basically, how can I get the index of the folder with the most recent timestamp?
I think I found a way to do it. I will try it and will update this post
oh my bad, missed that part.
import os
input_path = "C:\\Images\\ANGLE\\2021\\12\\20\\"
fileList = os.listdir(input_path)
tstamps =[(i,int(x.split('_')[-1])) for i,x in enumerate(fileNames)]
print fileList[max(tstamps[0])]
edit: I had to add a little bit to make sure max is selected without the influence of the index in the tuple
import os
from operator import itemgetter
input_path = "C:\\Images\\ANGLE\\2021\\12\\20\\"
fileNames = os.listdir(input_path)
tstamps =[(i,int(x.split('_')[-1])) for i,x in enumerate(fileNames)]
print fileNames[max(tstamps,key=itemgetter(1))[0]]
1 Like
Thank you @dkhayes117! I am not quite sure what happened at the last part of the code.
print fileNames[max(tstamps,key=itemgetter(1))[0]]
The file names is also dynamic:
so as an example it will be something like:
“C:\Images\ANGLE\2021\12\20\Pass_barcode123_095900”
“C:\Images\ANGLE\2021\12\20\Pass_barcode848_085900”
Is it possible to point to a folder name that contains the max timestamp value?
What value do you expect to be returned in this case?
Sorry it was a typo. The example should be:
“C:\Images\ANGLE\2021\12\20\Pass_barcode123_095900”
“C:\Images\ANGLE\2021\12\20\Pass_barcode848_085900”
and I want to return the path directory that has the most recent timestamp in this case the first one.
Like this?
print input_path + fileNames[max(tstamps,key=itemgetter(1))[0]]
just as an example
from operator import itemgetter
input_path = "C:\\Images\\ANGLE\\2021\\12\\20\\"
fileNames = ['Barcode_084000','Barcode_095900','Barcode_085900']
tstamps = [(i,int(x.split('_')[-1])) for i,x in enumerate(fileNames)]
print fileNames
print tstamps
print input_path + fileNames[max(tstamps,key=itemgetter(1))[0]]
>>>
['Barcode_084000', 'Barcode_095900', 'Barcode_085900']
[(0, 84000), (1, 95900), (2, 85900)]
C:\Images\ANGLE\2021\12\20\Barcode_095900
1 Like
Thank you very much @dkhayes117. Now it makes more sense. Thanks for the clarification!
I am going to try it with my file names and see what will happen.
That actually worked!!! Thank you so much! I learned something new here!
Could you please update the “fileList” name at the 3rd line to “fileNmaes” in your solution?
Appreciate it!
1 Like
You should also check out 10.1. os.path — Common pathname manipulations — Python 2.7.18 documentation. It has a lot of cool methods for file paths like
os.path.getctime(filePath)
1 Like
Very intresting, Daniel! I will check that out. Thank you!
1 Like