How to open and read an unknown extension file

Kushal Kumaran kushal.kumaran+python at
Thu Apr 8 14:30:41 EDT 2010

On Thu, Apr 8, 2010 at 10:39 PM, varnikat t <varnikat22 at> wrote:
> Hey,
> Thanks for the 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(
>         else:
>             file=open(glob.glob("*.*.html"))
>             self.text_view.get_buffer().set_text(
> This gives error!!

glob.glob returns a list of filenames.  You need to loop over it and
pass individual elements to the open function.

for item in glob.glob('*.txt'):
    # item is a filename.  pass it to open and process however you need

I don't know how the set_text method works, but it sounds like it
might not work right if you call it repeatedly with different


More information about the Python-list mailing list