accessing a text file

Zhu Sha Zang zhushazang at yahoo.com.br
Sun Sep 5 19:00:10 EDT 2010


 Em 05-09-2010 19:06, Alexander Kapps escreveu:
> 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
>
>
> Completely untested:
>
> def is_valid_word(word, filename):
>     for line in open(filename):
>         if word in line.split():
>             return True
>     return False

Damn, <ironic>easy like C/C++/Java</ironic>

Thanks a lot!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100905/10c49f1a/attachment-0001.sig>


More information about the Python-list mailing list