Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to open and read an unknown extension file

11 views
Skip to first unread message

Kushal Kumaran

unread,
Apr 8, 2010, 11:42:52 AM4/8/10
to varnikat t, python mailing
On Thu, Apr 8, 2010 at 9:00 PM, varnikat t <varni...@gmail.com> wrote:
> I am trying to do this
> if os.path.exists("*.*.txt"):
>             file=open("*.*.txt")
>             self.text_view.get_buffer().set_text(file.read())
> else:
>             file=open("*.*.html")
>             self.text_view.get_buffer().set_text(file.read())
>
> It gives error *.*.txt not existing....There are two files in the folder
> testing.pnm.txt
> and testing.pnm.html
> How to make it open any name and extension file and read it?
>

os.path.exists does not do pattern matching like that. Take a look at
the glob module.

--
regards,
kushal

Chris Colbert

unread,
Apr 8, 2010, 11:57:17 AM4/8/10
to Kushal Kumaran, python mailing, varnikat t
> --
> http://mail.python.org/mailman/listinfo/python-list
>

In [4]: files = [f for f in os.listdir(os.getcwd()) if
os.path.splitext(f)[-1]=='.txt']

In [5]: files
Out[5]:
['pip-log.txt',
'extended_abstract.txt',
'testlog.txt',
'pymazon_error_log.txt',
'hny.txt']

Steven Howe

unread,
Apr 8, 2010, 12:39:23 PM4/8/10
to pytho...@python.org
seems like a lot of work

from glob import glob

mylist = glob( "*.txt" )
for item in mylist:
print item

Kushal Kumaran

unread,
Apr 8, 2010, 2:30:41 PM4/8/10
to varnikat t, Python User
On Thu, Apr 8, 2010 at 10:39 PM, varnikat t <varni...@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

Tim Chase

unread,
Apr 8, 2010, 8:35:12 PM4/8/10
to varnikat t, python mailing
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

0 new messages