# Index of Max value

**URL:** <https://forum.inductiveautomation.com/t/index-of-max-value/54777>\
**Category:** Ignition\
**Created:** [January 7, 2022, 6:20pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777 "2022-01-07T18:20:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 6:20pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/1 "2022-01-07T18:20:40Z")

</div>

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)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/4/348a62be1d7dbc96202cf2301e192afce337c21f.png)

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?

```auto
	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)

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 7, 2022, 6:33pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/2 "2022-01-07T18:33:54Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 6:39pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/3 "2022-01-07T18:39:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 6:43pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/4 "2022-01-07T18:43:05Z")

</div>

I think I found a way to do it. I will try it and will update this post

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 7, 2022, 6:43pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/5 "2022-01-07T18:43:22Z")

</div>

oh my bad, missed that part.

```auto
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

```auto
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]]

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 6:54pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/6 "2022-01-07T18:54:34Z")

</div>

Thank you @dkhayes117! I am not quite sure what happened at the last part of the code.

```auto
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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 7, 2022, 6:56pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/7 "2022-01-07T18:56:56Z")

</div>

> [@pkhoshroo](#):
>
> “C:\Images\ANGLE\2021\12\20\Pass\_barcode123\_095900”  
> “C:\Images\ANGLE\2021\12\20\Pass\_barcode848\_095900”

What value do you expect to be returned in this case?

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 6:58pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/8 "2022-01-07T18:58:15Z")

</div>

Sorry it was a typo. The example should be:

```auto
“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.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 7, 2022, 7:00pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/9 "2022-01-07T19:00:00Z")

</div>

Like this?

```auto
print input_path + fileNames[max(tstamps,key=itemgetter(1))[0]]

```

just as an example

```auto
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]]

```

```auto
>>> 
['Barcode_084000', 'Barcode_095900', 'Barcode_085900']
[(0, 84000), (1, 95900), (2, 85900)]
C:\Images\ANGLE\2021\12\20\Barcode_095900

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 7:01pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/10 "2022-01-07T19:01:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 7:06pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/11 "2022-01-07T19:06:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 7, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/12 "2022-01-07T19:08:02Z")

</div>

You should also check out [10.1. os.path — Common pathname manipulations — Python 2.7.18 documentation](https://docs.python.org/2.7/library/os.path.html). It has a lot of cool methods for file paths like

```auto
os.path.getctime(filePath)

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [January 7, 2022, 7:09pm UTC](https://forum.inductiveautomation.com/t/index-of-max-value/54777/13 "2022-01-07T19:09:31Z")

</div>

Very intresting, Daniel! I will check that out. Thank you!
