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.
nicky sAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Florian LenschowCommented:
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.
0
aikimarkCommented:
why wouldn't it return 3.19?
0
aikimarkCommented:
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.
1
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Florian LenschowCommented:
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.
0
aikimarkCommented:
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

0
aikimarkCommented:
@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.
0
nicky sAuthor Commented:
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
0
aikimarkCommented:
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

0
aikimarkCommented:
Last night, a fellow Pythonista suggested there might be a more Pythonic way of writing this.  Based on his suggestion, here is another version of my function:
import os

def get_max_build_number(folder):
    return max([f[6:-4] for f in os.listdir(folder)], 
                key = lambda x: list(map(int, x.split("."))) )

Open in new window

==============================
@nicky
Have you tested the code we've posted?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
a  Pythonic solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.