Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python re.search error TypeError: expected string or buffer

Why would

re.search("\.docx", os.listdir(os.getcwd()))

yield the following error?

TypeError: expected string or buffer

like image 512
wolfsatthedoor Avatar asked Apr 06 '14 16:04

wolfsatthedoor


2 Answers

Because os.listdir returns a list but re.search wants a string.

The easiest way to do what you are doing is:

[f for f in os.listdir(os.getcwd()) if f.endswith('.docx')]

Or even:

import glob
glob.glob('*.docx')
like image 172
anon582847382 Avatar answered Nov 18 '22 11:11

anon582847382


re.search() expects str as the second argument. Refer docs to know more.

import re, os

a = re.search("\.docx", str(os.listdir(os.getcwd())))
if a:
    print(True)
else:
    print(False)
like image 40
ajknzhol Avatar answered Nov 18 '22 11:11

ajknzhol