[Tutor] search the folder for the 2 files and extract out only the version numbers 1.0 and 2.0.

Serdar Tumgoren zstumgoren at gmail.com
Tue Nov 17 00:21:09 CET 2009


> Aim:
> what is the best way to look into the allFiles directory, and only search
> the folder for the 2 files myTest1.0.zip and myTest2.0.zip to extract out
> only the version numbers 1.0 and 2.0.
>
Based on the above file-naming convention, you could get at the
version numbers with a combo of string methods and regular
expressions:

>>> import re  #import the regular expression module
>>> file_list = ['myTest1.0.zip', 'myTest2.0.zip']
>>> for f in file_list:
...         if f.startswith("myTest") and f.endswith("zip"):
...             version = re.search(r'\d+\.\d+', f).group()
...             print version
...
1.0
2.0
>>>

The above regular expression pattern searches the string for one or
more digits, followed by a dot (which has been escaped), followed by
another sequence of one or more digits. The group method returns the
match.

You could also skip the string methods (startswith/endswith) entirely
and just use a regular expression:

>>> for f in file_list:
...         version = re.match(r'myTest(\d+\.\d+)\.zip', f).group(1)
...         print version
...
1.0
2.0
>>>

You can tweak the regex pattern above if you need more
flexibility/fuzziness in the types of file names you're trying to
match.

No doubt others on the list will have more elegant solutions.
Meantime, this site provides a gentle intro to regular expressions:

http://www.amk.ca/python/howto/regex/

HTH!

Serdar


More information about the Tutor mailing list