How to open and read an unknown extension file

Kushal Kumaran kushal.kumaran+python at gmail.com
Thu Apr 8 20:30:41 CEST 2010


On Thu, Apr 8, 2010 at 10:39 PM, varnikat t <varnikat22 at gmail.com> wrote:
> Hey,
> 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())
>
> 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
filenames.

-- 
regards,
kushal



More information about the Python-list mailing list