accessing a text file

Baba raoulbia at gmail.com
Mon Sep 6 10:10:28 CEST 2010


On 6 sep, 00:04, Seth Rees <s... at sthrs.me> wrote:
> On 09/05/10 16:47, Baba wrote:
>
> > level: beginner
>
> > how can i access the contents of a text file in Python?
>
> > i would like to compare a string (word) with the content of a text
> > file (word_list). i want to see if word is in word_list. let's assume
> > the TXT file is stored in the same directory as the PY file.
>
> > def is_valid_word(word, word_list)
>
> > thanks
> > Baba
>
>    f = open('text.txt')
>    data = f.read()
>    # You may want to convert it to a list
>    data = data.split()
>    # Returns true if word is in data, false otherwise
>    word in data

Thanks Seth!



More information about the Python-list mailing list