0

everyone, I have problems with reading the file, the result of the function returns the old data (possibly cached). For example, the argument which parsed differs from the real. How to disable any cache in python and always get valid result? Here is my code:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os

file_name = 'file.conf'

if os.path.exists(file_name) : 
    with open(file_name, 'r') as f :
        b = bool(f.read().split()[2])
        if b == True:
            print('Result is true!' )
        elif b == False :
            print('Result is false!')
        else :
            print('Parse error!')
else :
    with open(file_name, 'w') as f:
        f.write('display_message_startup = False')

Python 3.5.2, Ubuntu 16.04.3 x86_64

D. Popov
  • 21
  • 5
  • Cna you post an example of input and expected output? There are several problematic lines in your code but without more info, it's hard to answer. – alec_djinn Nov 29 '17 at 15:27
  • `if b: ... elif not b: ... else: ...` - that one made me chuckle :D – Jeronimo Nov 29 '17 at 16:26
  • Sorry, not seen, I played with block before posting, but now I fix it. No changes in output. Any suggestions? – D. Popov Nov 29 '17 at 16:34
  • The way you wrote it, `b` will always be `True`, no matter the content of the file, since you `bool()` a string with single character in it, whereas only the null-string will evaluate to `False`. In fact, `"0"`, `"False"`, `"None"` ... they are all strings with more than zero chars and therefore `True`. As alec said, it'd help if you showed an example of your input and what you expect from that. – Jeronimo Nov 29 '17 at 16:36
  • I try to print what's comparing and understood as you said about my string always returns ``True`` and empty string returns ``False``. I used solution like this [link](https://stackoverflow.com/a/715468/8315512). By creating function for valid conversion from str to bool – D. Popov Nov 29 '17 at 17:08

0 Answers0