[Tutor] Zipfile and File manipulation questions.

Kent Johnson kent37 at tds.net
Sat Oct 14 23:59:53 CEST 2006


Bill Burns wrote:
> Maybe take a look at 'endswith':
> 
> Example:
> for filename in zfile.namelist():
>      if filename.endswith('.exe') or filename.endswith('.txt'):
>          # do something with filename

In Python 2.5 endswith() can take a tuple of strings to try:
   if filename.endswith(('.exe', '.txt')):

(The double parentheses create a single, tuple argument instead of 
passing two string arguments.)

And yes, endswith() is case-sensitive; use e.g.
   if filename.lower().endswith(('.exe', '.txt')):

Kent



More information about the Tutor mailing list