I am new to Stackoverflow so I will try to ask the question as detailed as possible. I am facing an issue where I call a function to write a value to a python file, and another function to return this written value. However, the function that supposed to return this written value does not return the most updated value after the write function.
I have done multiple researches and tried multiple methods from other questions posted in Stackoverflow but it still didn't work for me.
Here is what I currently have:
In test.py
import random
import string
from test_value import *
def write():
letters = string.ascii_letters
value_to_be_passed = ''.join(random.choice(letters) for i in range(10))
f = open("test_value.py", "w")
f.write('value="' value_to_be_passed '"')
f.close()
def read():
print(value)
write()
read()
In test_value.py
value="MutmHDlVQj"
If you run this file, it will generate a new string and store this new string into test_value.py. But when the function read() runs, it does not print out the newly generated string. It seem as though when I run this program, python will immediately store the value in test_value.py in it's memory and does not return the new value when called.
I have tried fsync, flush, with open but it still doesn't work. It won't be ideal to read the file as a plaintext as I might be storing multiple variables in test_value.py so that my program can call the value of the respective variables.
You can run the code in your ide, upon the first run it will print MutmHDlVQj in your terminal even though the goal is supposed to print the newly generated string.
Does anyone know how this could be solved? Thank you in advance.
Workaround Solution
Thanks to tripleee that gave me the idea of this cheap solution. However, I am sure there are better methods that can be done to solve this issue. May the experts give your opinions on this!
Instead of importing at compile time, I ran the importing when I really needed it.
def read():
from test_value import *
print(value)
CodePudding user response:
The import
happens when your script is compiled, before it runs and writes anything to the new file. If you want to import
something during runtime, you can do that with importlib
; but this seems fundamentally like an XY problem. If your task is to serialize some data, use pickle
or simply save the data in a standard format like JSON.
CodePudding user response:
from import method you can't pass a variable so simply for a return value, you must specify in test_value.py a return function. here's the functioning code:
import random
import string
from test_value import value
def write():
letters = string.ascii_letters
value_to_be_passed = ''.join(random.choice(letters) for I in range(10))
f = open("test_value.py", "w")
f.write('value="' value_to_be_passed '"')
f.close()
def read():
returned_value = value()
print(returned_value)
write()
read()
in test_value.py:
def value():
value="SbjvLSYfNs"
return value
written like this I have a functioning code that prints the value returned from another python file like you asked.
hope it helps in the future, but remember that if you want to return a value, you must specify the return function