TypeError: expected a character buffer object with tuple passed to Python 2.4 str.endswith()

161 Views Asked by At

My goal is to grab files with names ending with any item in ext. I have the following code:

ext=["n.log",".shm","rage"]

for filename1 in os.listdir(os.getcwd()):  
    if filename1.endswith(tuple(ext)):         
          src=os.getcwd()+'/'+filename1  
          irun_log=os.path.basename(src)

Running this code with Python 2.4, I get the following error:

Traceback (most recent call last):
  File "./compile", line 141, in ?
    if filename1.endswith(tuple(ext)):
TypeError: expected a character buffer object

Any help to figure out this issue is appreciated.

1

There are 1 best solutions below

0
Zero Piraeus On

The ability to pass a tuple of strings to str.endswith() to was introduced in Python 2.5:

str.endswith(suffix[, start[, end]])

Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

Changed in version 2.5: Accept tuples as suffix.