Avatar of nicky s
nicky s
Flag for United States of America asked on

Need Python Script for following requirement

Need Python script

Given a folder of build files that might look like this:

/Build-1.dmg
/Build-2.dmg
/Build-2.1.dmg
/Build-2.1.1.dmg
/Build-3.dmg
/Build-3.2.dmg
/Build-3.19.dmg

Write a script that takes a path to the folder as an argument, and prints the highest build number (just the number, i.e. 2.1.1)
Note:  The first digit can go up to 999 and should always be present, but the second and third can only go up to 99, and might not be included.
Python

Avatar of undefined
Last Comment
aikimark

8/22/2022 - Mon
Florian Lenschow

Not to elegant, since some things are hard coded but this should do the trick:
import os

def get_max_build_number(folder):
	max_build_number = [0,0,0]
	for filename in os.listdir(folder):
		if filename.endswith('.dmg'):
			number = os.path.splitext(filename[6:])[0]
			nums = number.split('.')
			if len(nums) == 1:
				nums.extend([0,0])
			elif len(nums) == 2:
				nums.extend([0])
			if int(nums[0]) > max_build_number[0]:
				max_build_number[0] = int(nums[0])
				if int(nums[1]) > max_build_number[1]:
					max_build_number[1] = int(nums[1])
					if int(nums[2]) > max_build_number[2]:
						max_build_number[2] = int(nums[2])
	return '.'.join(str(x) for x in max_build_number)

Open in new window

Also I would suggest to include zeros in the build names to make the naming convention a bit clearer.
aikimark

why wouldn't it return 3.19?
aikimark

I created some empty files with the sample names you provided and ran this code:
import os
files=[f for f in os.listdir('C:\Users\Mark\Downloads\Q_29096318')]
#print files
#print max(files)
print max([map(int,x[6:-4].split(".")) for x in files])

Open in new window

It produced the following output:
[3, 19]

Open in new window

Note: The two commented lines were there for diagnostic purposes. I needed to ensure that I was retrieving the file names and that the max() function returned incorrect results on string lists.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Florian Lenschow

aikimark's solution is more elegant. I haven't thought about using the max function so I compared the values manually, but I forgot another condition that resets the minor version numbers when a greater number is found in first position.
aikimark

If you need to assemble the result of the max() function into a string, do something like this:
m = max([map(int,x[6:-4].split(".")) for x in files])
print '.'.join(str(x) for x in m)

Open in new window

aikimark

@nicky s

I'm awaiting your response to my question about the correct max version number of your posted sample.  I can easily convert my code into a function, but I want to know that it is producing the correct output.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
nicky s

ASKER
It should return, 3.19 for the below files.

/Build-1.dmg
/Build-2.dmg
/Build-2.1.dmg
/Build-2.1.1.dmg
/Build-3.dmg
/Build-3.2.dmg
/Build-3.19.dmg
aikimark

import os

def get_max_build_number(folder):
    files=[f for f in os.listdir(folder)]
    m = max([list(map(int,f[6:-4].split("."))) for f in files])
    return '.'.join(str(x) for x in m)

Open in new window

ASKER CERTIFIED SOLUTION
aikimark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
aikimark

a  Pythonic solution
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck