1

In my code, I am trying to check a specific line of a config file by using linecache to read the line and an if statement to see if it is True, but for some reason it refuses to work.

I simplified the code in order to test it.

import linecache

d = linecache.getline('logconfig.dat', 2)

print(d)
if d == True:
    doeslock = True
else:
    doeslock = False

print(doeslock)

No matter what I try, print(d) will print True, and print(doeslock) will print False. I even tried using letters and strings instead of bools. Still didnt work. What am I missing here?

Thanks in advance guys

EDIT:

When I used strings to do the comparison, I replaced the True in the file with a y and modified the if statement to see if the d variable is 'y'

EDIT 2:

Ok guys I found the problem. Turns out the linecache returned both the line I want and the previous one for whatever reason. I separated the config into two files and now it works fine. No idea what caused this to happen but oh well. Thanks for the help!

3
  • What does print(repr(d)) print? Commented Dec 19, 2014 at 15:56
  • 2
    You say "I even tried using letters and strings", but you don't show us what you tried. So it remains the most likely cause. Commented Dec 19, 2014 at 15:56
  • As @kojiro says, we know that the ('True' == 'True') comparison will evaluate to the logical True. So we need you to show us the entire context of your program. There are things that might have subtle changes to the behavior of the comparison or other system features that you may unintentionally invoke. Commented Dec 19, 2014 at 16:04

5 Answers 5

3

When you read a line from a file, you get the whole line, including the newline character, as a string. You should strip that off and then compare with another string:

if d.strip('\n') == "True":
Sign up to request clarification or add additional context in comments.

9 Comments

This is a good point, and probably accounts for the "I tried using strings" confusion.
Even better to use d.strip() to cover some other possible whitespace.
@Lawrence or just "True" in d
That's not it. The file is written through a different script that I wrote. It doesn't include a '\n' in the write function
@ThsiseFaek provide code that demonstrates writing to a file without appending a newline. Further please prove that when that when linecache reads that file the result has no newline.
|
2

The string is not the same as the boolean value. Consider:

>>> d = 'True'
>>> print(d)
True
>>> if d == True:
...   doeslock = True
... else:
...   doeslock = False
... 
>>> print(doeslock)
False
>>> bool('False')
True

What you probably want is:

import linecache

d = (linecache.getline('logconfig.dat', 2)).strip()

print(d)
doeslock = (d == 'True')

print(doeslock)

Also consider the following:

>>> with open('randfile', 'w') as whatever:
...   whatever.write('y')
... 
>>> import linecache
>>> d = linecache.getline('randfile', 1)
>>> print(d)
y

>>> d == 'y'
False
>>> 'y' in d
True
>>> d.strip() == 'y'
True

Comments

0

linecache.getline returns a string, not a boolean. So you have to compare d to "True". You also may have to strip the result of getline in case there are spaces or newline.

>>> if d=="True":
...   print 'ok'
...
ok

1 Comment

Like I said in the post, I thought of that and tried using strings instead of bools. It still didn't work.
0

try if d.strip() == 'True': may be you have an extra space or a \n.

Comments

0

import linecache

d = linecache.getline('logconfig.dat', 2)

print(d)

if d:

doeslock = True

else:

doeslock = False

print(doeslock)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.