0

I need a little help with python.

I have written a small program and would now like to store a bool value outside the program, so that a certain action in the program is only executed if the bool was True or False.

It is important that the value is retained even if the program is not running. In addition, the bool must be able to be set when the program is running.

I am grateful for any help.

I hope I get an idea now on how exactly this can be done.

Aaron Chauhan
  • 15
  • 1
  • 6
Rainer
  • 1
  • 2

2 Answers2

0

You can use any config file to store the bool value( you can use normal files too but it depends on context). I doesn't take you long to find a good library to manage config file of any type in python.

You can also use env to store the bool value but i don't recommend it.

Nishanth P
  • 21
  • 1
  • 2
0

You can import another python file with the bool

  • Create another python file (I'll call it myboolscript.py for now)
  • Add your bool to myboolscript.py: myboolvar = True
  • Add the import in you main script: from myboolscript import myboolvar
  • Now you can use it anywhere as a variable

Example:

main.py

from myboolscript import myboolvar

print("Hello World!")

if myboolvar == True:
  print("Bool is True")

myboolscript.py

myboolvar = True

Output:

Hello World!
Bool is True

You can also use CSV files

It is worth noting that using a python script may be easy, but isn't very efficient. There are plenty of other ways to do it, like using a CSV file.

Here's a post on it: What is a convenient way to store and retrieve boolean values in a CSV file

Aaron Chauhan
  • 15
  • 1
  • 6