Need Python Script for following requirement

nicky s
nicky s used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.
Top Expert 2014

Commented:
why wouldn't it return 3.19?
Top Expert 2014

Commented:
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.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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.
Top Expert 2014

Commented:
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

Top Expert 2014

Commented:
@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.

Author

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
Top Expert 2014

Commented:
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

Top Expert 2014
Commented:
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?
Top Expert 2014

Commented:
a  Pythonic solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial