nicky s
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.
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.
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])
It produced the following output:[3, 19]
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)
@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.
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.
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
/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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a Pythonic solution
Open in new window
Also I would suggest to include zeros in the build names to make the naming convention a bit clearer.