Just wondering if someone can be kind enough to tell me what I am doing wrong. working on a bit of code, with use with my cherrypy project
import glob
import os
def get_html(apath):
print (apath)
fhtml = open(apath,'r')
data = fhtml.read()
return data
article_path = os.getcwd() +'/articles/*.html'
myArticles = [glob.glob(article_path)]
print len(myArticles)
for items in myArticles:
get_html(items)
Results in:
['/home/sd/Documents/projects/cherryweb/articles/Module 5-Exploitation Techniques And Exercise.pdf.html']
Traceback (most recent call last):
File "fntest.py", line 22, in <module>
get_html(items)
File "fntest.py", line 10, in get_html
fhtml = open(apath,'r')
TypeError: coercing to Unicode: need string or buffer, list found
I assume its because the filename I'm passing has the [' and '] from list on the string, I could write a function to trim these parts off, but it that the only option, or am I doing something stupid.
Thanks
myArticles = [glob.glob(article_path)]
should be:
myArticles = glob.glob(article_path)
glob.glob
returns a list, by adding []
around it you made it a list of list.
So, in this loop:
for items in myArticles:
get_html(items)
you actually passed the whole list to get_html
and open
raised that error.
Demo:
>>> open([])
Traceback (most recent call last):
File "<ipython-input-242-013dc85bc958>", line 1, in <module>
open([])
TypeError: coercing to Unicode: need string or buffer, list found