-2

here's the problem: i have a file ,,file.txt''.It is compossed by 11 words. Good. Now i have the code :

with open('/root/file.txt', 'r') as f:
 data = f.readlines()
 print data[10]

It outputs :

Password

But when i enter :

if data[10] == 'Password':
 print 'yes'
else:
 print 'no'

It outputs:

no

Can i know why ?I alredy tried to do ,,str(data[10])" but i get the same output : no. How i can do to get the yes answere ?

2 Answers2

1

with readlines() you get a list with each line as a member of the list, this include the \n (return line code).

Print the whole list to see what I am saying.

with open('/root/file.txt', 'r') as f:
 data = f.readlines()
print data

To avoid the \n use this code:

data[10].strip() == 'Password'

If you just need to print something if the word Password is in there, try this code:

if 'Password' in data[10]:
  print 'yes'
-1

This happens because in file "password" is stored as "password\n"

https://i.stack.imgur.com/CcIJ7.png