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

accessing a text file

1 view
Skip to first unread message

Baba

unread,
Sep 5, 2010, 5:47:33 PM9/5/10
to
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

Seth Rees

unread,
Sep 5, 2010, 6:04:12 PM9/5/10
to

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

Benjamin Kaplan

unread,
Sep 5, 2010, 6:01:19 PM9/5/10
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Please do us a favor and at least try to figure things out on your
own, rather than coming here every time you have a question. The very
first result when you try searching "python read text file" is the
section in the Python tutorial that explains how to do this.

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

MRAB

unread,
Sep 5, 2010, 6:05:12 PM9/5/10
to Python List
On 05/09/2010 22:47, Baba wrote:
> level: beginner
>
> how can i access the contents of a text file in Python?
>
That's a very basic question.

I suggest you read a tutorial such as "Dive Into Python":

http://diveintopython.org/toc/index.html

Alexander Kapps

unread,
Sep 5, 2010, 6:06:49 PM9/5/10
to pytho...@python.org


Completely untested:

def is_valid_word(word, filename):
for line in open(filename):
if word in line.split():
return True
return False

Zhu Sha Zang

unread,
Sep 5, 2010, 7:00:10 PM9/5/10
to pytho...@python.org
Em 05-09-2010 19:06, Alexander Kapps escreveu:
> 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!

signature.asc

Baba

unread,
Sep 6, 2010, 3:55:28 AM9/6/10
to
On 6 sep, 00:01, Benjamin Kaplan <benjamin.kap...@case.edu> wrote:
> http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-...


Hi Benjamin

I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner.
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.


To all other respondants: thank you for your kind instructions and
directions.


Thanks
Baba

Baba

unread,
Sep 6, 2010, 4:10:28 AM9/6/10
to

Thanks Seth!

Thomas Jollans

unread,
Sep 6, 2010, 10:58:39 AM9/6/10
to pytho...@python.org
On Monday 06 September 2010, it occurred to Baba to exclaim:

That is of course perfectly legitimate. It would however have been polite to
state that in the question. Show us that you're doing your homework, and not
just using the list as a cheap path to having to think less yourself.

Phrasing your post as "I would like to compare a ... I found the open()
function here: [link]. Is this what I should use of is there any other/better
way?" makes a completely different impression.

Also, keeping personal comments to one's self is just not how it works. On a
list like this especially, answers along the lines of "That's the way to do
what you were asking for, but are you sure the question went into the right
direction? Have you thought of [...]?" can often be very helpful.

Baba

unread,
Sep 6, 2010, 11:53:53 AM9/6/10
to

Thanks Thomas. Look up some of my questions this group and read
through them and come back to tell me if a) i use this forum to learn
without making any efforts myself or b) i use this forum to get
started using the expertise of more knowledgeable programmers while at
the same time particiapting. Anyway having this discussion is beside
the point. Any Expert out there who thinks we beginners are some dumb
idiots who are too stupid to think for themselves and are lucky to
have a bunch of geniuses like you to help, get lost or make yourself a
cup of tea but please give me a break from teaching me lessons...

There's lots of nice people out there who are being awsemome by
dedicating a bit of their time to some of our easy questions and it is
them that i wish to send a BIG thanks! Much appreciated. I have made
some good progress since starting to learn Python and this group has
been great help!

tnx
Raoul

geremy condra

unread,
Sep 6, 2010, 12:14:41 PM9/6/10
to Baba, pytho...@python.org

Just a quick point- when you ask someone for help, it's considered
impolite to tell them what to do.

I'd also point out that you gave no indication that you'd worked on
this at all before posting it. In that regard, Thomas's concern seems
completely justified to me.

> or b) i use this forum to get
> started using the expertise of more knowledgeable programmers while at
> the same time particiapting.

Again, Thomas's concern seems justified to me. Things would probably
go more smoothly if you gave a better indication of what you had done
so far on the problem in the future.

> Anyway having this discussion is beside
> the point. Any Expert out there who thinks we beginners are some dumb
> idiots who are too stupid to think for themselves and are lucky to
> have a bunch of geniuses like you to help, get lost or make yourself a
> cup of tea but please give me a break from teaching me lessons...

I don't think all beginners are idiots, or even most of them- but this
isn't the right attitude to be taking. Both Thomas and myself thought
that this was inappropriate enough to mention it, and if two people
spoke up you can bet a lot more were thinking it quietly. My
suggestion would be to moderate your approach and demonstrate what
you've done so far (if only to increase the signal-to-noise ratio as
your problems become more challenging) when posting. I'd also refrain
from telling people to get lost; it doesn't make people happy to help
you, you know?

Geremy Condra

Baba

unread,
Sep 6, 2010, 12:29:15 PM9/6/10
to
On 6 sep, 18:14, geremy condra <debat...@gmail.com> wrote:

Thanks Jeremy, i will take your advice on board! Noone likes to be
taught lessons i think so it is only normal that i reacted. If i had
received a friendly response from Benjamin (as opposed to "Please do
us a favor and at least try to figure things out on your own") making
me aware of the etiquette that my post should also show that i have
researched my question somehow and if his tone would have been
mannered then we would not be having this discussion now. Ok now i
need to go back to actual Pythoon learning, i'm getting distracted.

Kind regards,
Baba

Ben Finney

unread,
Sep 6, 2010, 7:36:52 PM9/6/10
to
Baba <raou...@gmail.com> writes:

> Thanks Jeremy, i will take your advice on board! Noone likes to be
> taught lessons i think so it is only normal that i reacted.

Please reconsider this response. Many of us use this forum precisely
because we *do* like to be taught lessons. If you don't want to be
taught lessons, this is not the forum to use.

> If i had received a friendly response from Benjamin (as opposed to
> "Please do us a favor and at least try to figure things out on your
> own")

Again, please reconsider your perception of the messages you received.
Those messages are polite; they're even explicit about how you can
improve your future messages. They are polite enough to regard you as a
person who can improve.

We value respect for people here, and that's what you've been shown
consistently. But respect for opinions, or for delicacy about learning,
is not welcome here. In other words, we treat people as adults by
default. I hope you'll continue to participate in that spirit.

--
\ “I was gratified to be able to answer promptly and I did. I |
`\ said I didn't know.” —Mark Twain, _Life on the Mississippi_ |
_o__) |
Ben Finney

Ben Finney

unread,
Sep 6, 2010, 8:18:24 PM9/6/10
to
Ben Finney <ben+p...@benfinney.id.au> writes:

> We value respect for people here, and that's what you've been shown
> consistently. But respect for opinions, or for delicacy about
> learning, is not welcome here.

Sloppy wording, I apologise. This should say “… is not respect for a
person”.

> In other words, we treat people as adults by default. I hope you'll
> continue to participate in that spirit.

This is the main thrust of the message.

--
\ “What if the Hokey Pokey IS what it's all about?” —anonymous |
`\ |
_o__) |
Ben Finney

Baba

unread,
Sep 7, 2010, 7:39:37 AM9/7/10
to
On 7 sep, 02:18, Ben Finney <ben+pyt...@benfinney.id.au> wrote:

Yes Master :)

Baba

unread,
Sep 7, 2010, 7:59:01 AM9/7/10
to

Sloppy wording, I apologise. This should say: If you find the question
you're reading too easy then just don't answer. Noone is the owner of
a democratic forum where freedom to ask the question one likes is
paramount (as long of course as it is related to the group)...so let
me repeat that, to say "Please do us a favour and at least try to
figure things out on your own" is in my view inappropriate. To me it
sounds "Do me a favur and get lost". Can you not understand that? I
accept that it might not have been meant that way but see,
misunderstandings happen easily so a more subtle approach is a
wothwhile effort. My point: i always encourage people to get down
from their ivory towers and to connect to the people...yes they will
sometimes be lazy but they are genuine and very respectful (unlike
what you think)...

no offence now ok, it's not all that serious...open your mind, let
lose all that righteousness and let's enjoy life :)


Baba

Bruno Desthuilliers

unread,
Sep 7, 2010, 9:42:14 AM9/7/10
to
Baba a écrit :
(snip)

> If i had
> received a friendly response from Benjamin (as opposed to "Please do
> us a favor and at least try to figure things out on your own")

According to usenet standards and given your initial question, this is a
_very_ friendly answer.

Ben Finney

unread,
Sep 7, 2010, 10:37:29 AM9/7/10
to
Baba <raou...@gmail.com> writes:

> to say "Please do us a favour and at least try to figure things out on
> your own" is in my view inappropriate.

That's what the person wanted you to see. How would you prefer that
exact information to be imparted to you? How could it have been
communicated so that it was not misunderstood?

> To me it sounds "Do me a favur and get lost". Can you not understand
> that?

Honestly, I cannot read that meaning into the messages you've received
in this forum. Please help us by showing how you think people could say
what was said above without losing information.

> no offence now ok, it's not all that serious...open your mind, let
> lose all that righteousness and let's enjoy life :)

All well and good, but let's try to see what went wrong in communication
here and fix it.

--
\ “Faith, n. Belief without evidence in what is told by one who |
`\ speaks without knowledge, of things without parallel.” —Ambrose |
_o__) Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney

Grant Edwards

unread,
Sep 7, 2010, 10:50:05 AM9/7/10
to
On 2010-09-07, Baba <raou...@gmail.com> wrote:

> Sloppy wording, I apologise. This should say: If you find the
> question you're reading too easy then just don't answer. Noone is the
> owner of a democratic forum where freedom to ask the question one
> likes is paramount (as long of course as it is related to the
> group)...so let me repeat that, to say "Please do us a favour and at
> least try to figure things out on your own" is in my view
> inappropriate.

You need to read this:

http://www.catb.org/esr/faqs/smart-questions.html

> To me it sounds "Do me a favur and get lost".

No, it means "Do yourself a favor, learn how to do things yourself."

Remember: you're then one asking people to give you something for
free. It's not up to them to conform to your expectations, rather you
need to conform to theirs. Otherwise, they'll just ignore you.

--
Grant Edwards grant.b.edwards Yow! FOOLED you! Absorb
at EGO SHATTERING impulse
gmail.com rays, polyester poltroon!!

geremy condra

unread,
Sep 7, 2010, 12:48:52 PM9/7/10
to Baba, pytho...@python.org

This is in almost impressively poor taste. I know I'm done helping you now.

Geremy Condra

Baba

unread,
Sep 7, 2010, 1:45:52 PM9/7/10
to
On 7 sep, 16:50, Grant Edwards <inva...@invalid.invalid> wrote:

"Please do us a favour" sounds condescending to me at least but maybe
we Europeans are a bit touchy...

However the following Wiki excerpt seems to go in my direction:

"When someone makes a mistake -- whether it's a spelling error or a
spelling flame, a stupid question or an unnecessarily long answer --
be kind about it. If it's a minor error, you may not need to say
anything. Even if you feel strongly about it, think twice before
reacting. Having good manners yourself doesn't give you license to
correct everyone else. If you do decide to inform someone of a
mistake, point it out politely, and preferably by private email rather
than in public. Give people the benefit of the doubt; assume they just
don't know any better. And never be arrogant or self-righteous about
it."

http://en.wikipedia.org/wiki/Netiquette

Baba

Ben Finney

unread,
Sep 7, 2010, 8:07:19 PM9/7/10
to
Baba <raou...@gmail.com> writes:

> However the following Wiki excerpt seems to go in my direction:

No, it doesn't. It advises that people show kindness; as I've been
arguing, that's exactly what you were shown. You haven't shown how the
information being imparted could have been fully imparted in a way
that's kinder, nor that it would be reasonable to do so.

To put it another way: if you feel offended by an utterance, then
insufficient kindness on the part of the speaker is not the only
explanation.

--
\ “Software patents provide one more means of controlling access |
`\ to information. They are the tool of choice for the internet |
_o__) highwayman.” —Anthony Taylor |
Ben Finney

Message has been deleted

Baba

unread,
Sep 8, 2010, 6:30:00 AM9/8/10
to
On 8 sep, 02:07, Ben Finney <ben+pyt...@benfinney.id.au> wrote:

Hi Ben,

Thanks for your feedback. My question is: Who owns this forum? If we
all do then we are allowed to post questions that are simple and that
could otherwise be answered by doing research. It is just unfriendly
to tell someone to go and look it up by themselves. Who is licensed to
judge what can and cannot be posted as a question? A teacher of mine
used to say: "There are no stupid questions, there are only stupid
answers." It is arrogant to tell someone in a Forum to "look it up
yourself ,this question is too basic". Can you not understand that
accessing a file can seem daunting at first? Believe me, documentation
can be offputting too when you only start with programming. Per se the
beauty of forums like these is that there are human beings willing to
make such tasks as finding out how to access a text file less 'scary'.
Whoever thinks he or she has a license to tell someone to look up the
answer by themselves should think again. I reckon the only license we
have is not to answer at all. I acknowledge that Benjamin pointed me
to the right place to find an answer but somehow the "Please do us a
favour" sounded bit arrogant, as if he owned the place (same goes for
all those who sided against me here: do you own this forum?)...i'd be
glad if there was a python for beginners forum but in the meantime i
have to say i find it awesome to have this forum to get help. It makes
a BIG difference. So sorry if i upset anyone, we all have our opinions
and get carried away and can be stubborn, i DO respect everyone in
this forum and i think it goes bothways. I've learned a few things in
this post...

So now, to use Benjamin words: Please do me a favour and let's move
on :)

Baba

Baba

Paul Rubin

unread,
Sep 8, 2010, 6:46:18 AM9/8/10
to
Baba <raou...@gmail.com> writes:
> It is just unfriendly
> to tell someone to go and look it up by themselves.

Someone seeing too many unthoughtful questions from you might tell you
to look it up yourself, in the hopes of getting you to change your
questioning style, so that your future questions will be more thoughtful
and worth answering. But according to you that would be unfriendly.

Another thing they could do is say nothing, but quietly configure their
news-reading software to ignore your questions completely. Then none of
your future questions would have any hope of being answered. Would that
be less unfriendly, or more unfriendly?

Baba

unread,
Sep 8, 2010, 7:28:32 AM9/8/10
to
On 8 sep, 12:46, Paul Rubin <no.em...@nospam.invalid> wrote:

Hi Paul

I would support option 1 but phrased more thoughtfully than Benjamin
did (that way no feelings will be hurt):

"Dear xyz,
Your question can easily be researched online. We suggest you give it
a try and to look it up yourself. This will be beneficial both to you
and to us. We do encourage to ask questions only when they have been
researched first. We care about the quality of posts but we also
understand that as a beginner one can tend to look for an easy or
quick way to find answers. So in the meantime here's something to
get you started: link"

But where do you draw the line? Can we not just let people ask
questions regardless? And let those answer who want to and those who
don't just ignore the question? That seems so much easier to me.


Paul Rubin

unread,
Sep 8, 2010, 8:39:22 AM9/8/10
to
Baba <raou...@gmail.com> writes:
> But where do you draw the line? Can we not just let people ask
> questions regardless? And let those answer who want to and those who
> don't just ignore the question? That seems so much easier to me.

The first few times, it's easy to ignore the questions. After a few
more times, it becomes easier to ignore the person asking the questions,
and not even look at the questions.

The answer to many of your questions also is not "research it online",
but rather, "figure it out with your own creativity". That's what being
a programmer is about--figuring out solutions that don't already exist
elsewhere.

Grant Edwards

unread,
Sep 8, 2010, 10:57:32 AM9/8/10
to
On 2010-09-08, Baba <raou...@gmail.com> wrote:

> Thanks for your feedback. My question is: Who owns this forum? If we
> all do then we are allowed to post questions that are simple and that
> could otherwise be answered by doing research.

Of course you're allowed to post such questions.

And people are allowed to ignore you, to answer your question
sarcastically, to attempt to teach you how to answer your own
questions, or to respond in other ways.

It's up to _you_ to make the effort to try to insure that you get a
useful response. To that end here is how that is accomplished:

http://catb.org/esr/faqs/smart-questions.html

Maybe you don't _like_ the fact that you are expected to show just a
tiny bit of inititiative and care in researching and posting your
question, but that's pretty much the way it it works. I'm sure you'd
prefer that everything was handed to you for free on a silver platter
with a side order of beer and cookies. I'd prefer I was 20 years
younger and 30 pounds lighter. Life's tough that way.

> It is just unfriendly to tell someone to go and look it up by
> themselves.

1) It isn't if give them a hint on where to look it up.

2) Posting questions like yours is considered unfriendly.

> Who is licensed to judge what can and cannot be posted as a question?

Nobody. Post whatever questions you want however you want in whatever
language you want. If you don't care about actually _answering_ your
question, feel free to continue in the same vein in which you started.

If you _do_ care about getting a prompt, accurate answer, then:

http://catb.org/esr/faqs/smart-questions.html

> A teacher of mine used to say: "There are no stupid questions, there
> are only stupid answers."

Ask that teacher for help then.

> It is arrogant to tell someone in a Forum to "look it up yourself,


> this question is too basic".

Perhaps it is. But remember you're the one asking strangers for a free
service, so it's up to you to "play by the rules" in return for that
free service. One of the rules is that you first try to answer the
question yourself.

> Can you not understand that accessing a file can seem daunting at
> first?

Maybe so, but reading a tutorial isn't.

--
Grant Edwards grant.b.edwards Yow! I need to discuss
at BUY-BACK PROVISIONS
gmail.com with at least six studio
SLEAZEBALLS!!

Bruno Desthuilliers

unread,
Sep 8, 2010, 12:15:08 PM9/8/10
to
Baba a écrit :

>
> "Dear xyz,
> Your question can easily be researched online. We suggest you give it
> a try and to look it up yourself. This will be beneficial both to you
> and to us. We do encourage to ask questions only when they have been
> researched first."

On usenet - as well as on most technical forums / mailing lists / etc -,
this usually condensed in a well-known four letters acronym : "RTFM"

Hopefully c.l.py is an unusually friendly and polite place, so we tend
to say it a bit more elegantly and most of the time we do indeed provide
a link.

This being said and given your attitude - you may not realize it, but by
now you would have been flamed to hell and back on quite a few other
newsgroups -, I'm very tempted to switch to option 2.

Baba

unread,
Sep 8, 2010, 5:08:00 PM9/8/10
to
On 8 sep, 14:39, Paul Rubin <no.em...@nospam.invalid> wrote:

Hi Paul

If i look where i was 4 weeks ago and the progress i made in learning
Python i am quite delighted. This forum has helped me and i appreciate
it. I don't think i will ever tell a beginner to "do me a favour" and
to look things up by himself nor will i use the RTFM line (refering to
Bruno's post), i'd just be nice and helpful as everyone here is.
Didn't realise ego's were that big but so is mine i suppose...

kind regards
Baba

Bar Shirtcliff

unread,
Sep 8, 2010, 6:32:24 PM9/8/10
to Baba, pytho...@python.org
| Hi Paul
|
| If i look where i was 4 weeks ago and the progress i made in learning
| Python i am quite delighted. This forum has helped me and i appreciate
| it. I don't think i will ever tell a beginner to "do me a favour" and
| to look things up by himself nor will i use the RTFM line (refering to
| Bruno's post), i'd just be nice and helpful as everyone here is.
| Didn't realise ego's were that big but so is mine i suppose...
|

HEREow can you be learning so much python if you're constantly expressing
your personal tastes and generally posturing on the Python forum?

I know how words can capture one into a realm of pretended meaning and
significance. The nice thing about programming, as opposed to the
humanities, is that it's about actually doing things, or rather,
actually building things that do things for you.

Let's get to it!

Best,
Bar


--


----------------------------------------------------------------------
"All men are born mad. Some remain so."

--Samuel Beckett

Bar Shirtcliff

unread,
Sep 8, 2010, 6:56:11 PM9/8/10
to b...@cs.unm.edu, pytho...@python.org, Baba
| HEREow can you be learning so much python if you're constantly expressing

typo there. I'm not sure how that happens, sometimes, but it's an
untimely abbrev-expansion, in emacs VM.

I meant to say, "How can you..."

Cheers,
Bar


MRAB

unread,
Sep 8, 2010, 7:13:37 PM9/8/10
to Python List
An unkind soul would say that "it's a poor workman who blames his
tools", but as it's emacs... ;-)

Ben Finney

unread,
Sep 8, 2010, 9:31:04 PM9/8/10
to
Baba <raou...@gmail.com> writes:

> Thanks for your feedback. My question is: Who owns this forum? If we
> all do then we are allowed to post questions that are simple and that
> could otherwise be answered by doing research.

That's a rather subservient perspective. Why are you seeking permission
to ask questions? Why do you raise the issue of whether one is *allowed*
to ask questions?

If you take advice and requests as commandments, then I think that's
pretty close to the root of the communication failures here.

When someone asks you to change your behaviour, or describes the
possible consequences that your behaviour entails, they're treating you
as a responsible adult, not as a slave or infant.

> It is just unfriendly to tell someone to go and look it up by
> themselves.

Yes, to *tell* them to do it would be unfriendly. You, however, received
*requests* that you do so, with reasons why. Please help us all — and
you are included in “us all” — by learning the difference between a
command and a request.

--
\ “It is difficult to get a man to understand something when his |
`\ salary depends upon his not understanding it.” —Upton Sinclair, |
_o__) 1935 |
Ben Finney

Ben Finney

unread,
Sep 8, 2010, 9:33:18 PM9/8/10
to
Grant Edwards <inv...@invalid.invalid> writes:

> I'm sure you'd prefer that everything was handed to you for free on a
> silver platter with a side order of beer and cookies. I'd prefer I was
> 20 years younger and 30 pounds lighter. Life's tough that way.

Hell no. I'd prefer to have the total of my life experience and just
have my *body* be twenty years younger :-)

--
\ “The greatest tragedy in mankind's entire history may be the |
`\ hijacking of morality by religion.” —Arthur C. Clarke, 1991 |
_o__) |
Ben Finney

Grant Edwards

unread,
Sep 8, 2010, 10:03:15 PM9/8/10
to
On 2010-09-09, Ben Finney <ben+p...@benfinney.id.au> wrote:
> Grant Edwards <inv...@invalid.invalid> writes:
>
>> I'm sure you'd prefer that everything was handed to you for free on a
>> silver platter with a side order of beer and cookies. I'd prefer I was
>> 20 years younger and 30 pounds lighter. Life's tough that way.
>
> Hell no. I'd prefer to have the total of my life experience and just
> have my *body* be twenty years younger :-)

I'd have to think about that for a while. A little bit of innocent
optimism might be nice...

--
Grant

Nobody

unread,
Sep 9, 2010, 5:09:29 PM9/9/10
to
On Wed, 08 Sep 2010 03:30:00 -0700, Baba wrote:

> Who is licensed to judge what can and cannot be posted as a question?

Exactly the same set of people who are licensed to judge what can and
cannot be posted as an answer.

If you don't like the responses you get here, you could try posting your
questions on 4chan. If nothing else, that will give you a whole new
perspective on what an "unfriendly" response really looks like.

Paul Rubin

unread,
Sep 9, 2010, 5:35:12 PM9/9/10
to
Nobody <nob...@nowhere.com> writes:
> If you don't like the responses you get here, you could try posting your
> questions on 4chan. If nothing else, that will give you a whole new
> perspective on what an "unfriendly" response really looks like.

+1 QOTW

Baba

unread,
Sep 16, 2010, 6:36:10 AM9/16/10
to

I would to apologise to anyone who might have been upset or offended
by my reaction. I am new to forums (as i am new to programming) so i
hope i might be forgiven for not appreciating the true value of the
support one can receive on this forum. I wish to reiterate that i
underatand that experts provide help free of charge and in their spare
time so there's no point for me to like or dislike the style of an
answer.

Baba

0 new messages