radicaled radicaled - 2 years ago 148
Python Question

Split file name on python

I'm trying to split a filename use it by parts.

The file is always an apache binary (for example, httpd-2.2.31.tar.gz) and I only need the version (in this case 22) in that specific format.

I have done it and it works, but I believe there should be a better way

binary_name = "httpd-2.2.31.tar.gz"
app, ver_tar = binary_name.split("-")
version = ver_tar.split(".")
print ("Version %s%s" % (version[0], version[1]))

Is it possible to do it in a better way?

Answer Source

if you want to do it in one line,may be it will help :

binary_name = "httpd-2.2.31.tar.gz"
print binary_name.split("-")[1][:3].replace('.','')

will return 22

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download