0

I am learning how to program in python for school and I apologize if my question is very basic. But I am attempting to have python read a txt file on my macbook that is called word.txt for an exercise for a book called think_python for class. Below is the code I am attempting to use to have the file read by python

import os

f = open(os.path.expanduser("~/Downloads/word.txt"))

f.readlines()

I then run the in function to try and have python see if the word "uno" is in the word.txt file.

"uno" in f

Python returns the in function as false, but I do have the word as "uno" in the text file, I have also tried removing the quotes and just uno in the txt file and same result. The assignment asks that I return true indicating that python does see the word "uno" and returns true for the in function. Can someone tell me what I am doing wrong? thank you

Also when I do a print(f) I do see that python returns the following:

print(f)
<open file '/Downloads/word.txt', mode 'r' at 0x207ed10>

Does that mean that python is reading the file correctly?

Murat Seker
  • 890
  • 2
  • 14
  • 26
Tien
  • 1
  • Notwithstanding that this is probably a duplicate, you need to save what you read. I.E. f = f.readlines() and then "uno" in f. – chevydog Oct 28 '17 at 18:25
  • @chevydog thanks for the tip, I can get it to work with just the words listed in the txt file. The exercise asks that the txt file stores the values in the file as keys in a dictionary. I attempted to add the values uno and one in the txt file as shown below: f = dict() f[“one”] = “uno” Is what I am doing allowed for python to read the data as a key-value pair? thanks again for your help! – Tien Oct 28 '17 at 18:46

0 Answers0