Link to home
Start Free TrialLog in
Avatar of nicky s
nicky sFlag 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.
Avatar of Florian Lenschow
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.
Avatar of aikimark
why wouldn't it return 3.19?
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.
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.
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

@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.
Avatar of 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
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
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a  Pythonic solution