How to open and read an unknown extension file

Tim Chase python.list at tim.thechases.com
Fri Apr 9 02:35:12 CEST 2010


On 04/08/2010 12:22 PM, varnikat t wrote:
> it gives me this error
>
> TypeError: coercing to Unicode: need string or buffer, list found
>> Thanks for the help.it detects now using glob.glob("*.*.txt")
>> Can u suggest how to open and read file this way?
>>
>> *if glob.glob("*.*.txt"):
>>              file=open(glob.glob("*.*.txt"))
>>
>>              self.text_view.get_buffer().set_text(file.read())
>>          else:
>>              file=open(glob.glob("*.*.html"))
>>
>>              self.text_view.get_buffer().set_text(file.read())

glob() returns a list of matching files, not a string.  So you 
need to iterate over those files:

   filenames = glob.glob('*.*.txt')
   if filenames:
     for filename in filenames:
       do_something_text(filename)
   else:
     for filename in glob('*.*.html'):
       do_something_html(filename)

-tkc






More information about the Python-list mailing list