简体   繁体   English

文件写入后如何从另一个python文件中读取值?

[英]How to read a value from another python file after file write?

I am new to Stackoverflow so I will try to ask the question as detailed as possible.我是 Stackoverflow 的新手,所以我会尽量详细地提出这个问题。 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.我面临一个问题,我调用一个函数将一个值写入 python 文件,并调用另一个函数来返回这个写入的值。 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.我已经进行了多项研究,并尝试了 Stackoverflow 中发布的其他问题的多种方法,但它仍然对我不起作用。

Here is what I currently have:这是我目前拥有的:

In test.py在 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在 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.如果您运行此文件,它将生成一个新字符串并将此新字符串存储到 test_value.py 中。 But when the function read() runs, it does not print out the newly generated string.但是当函数 read() 运行时,它不会打印出新生成的字符串。 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.好像当我运行这个程序时,python 会立即将 test_value.py 中的值存储在它的内存中,并且在调用时不会返回新值。

I have tried fsync, flush, with open but it still doesn't work.我已经尝试过 fsync、flush、open 但它仍然不起作用。 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.将文件作为纯文本读取并不理想,因为我可能会在 test_value.py 中存储多个变量,以便我的程序可以调用各个变量的值。

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.您可以在您的 ide 中运行代码,第一次运行时它会在您的终端中打印MutmHDlVQj ,即使目标应该打印新生成的字符串。

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.感谢 Tripleee 让我想到了这个便宜的解决方案。 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)

The import happens when your script is compiled, before it runs and writes anything to the new file. import发生在您的脚本编译时,在它运行并将任何内容写入新文件之前。 If you want to import something during runtime, you can do that with importlib ;如果你想在运行时import一些东西,你可以使用importlib but this seems fundamentally like an XY problem .但这似乎基本上是一个XY 问题 If your task is to serialize some data, use pickle or simply save the data in a standard format like JSON.如果您的任务是序列化某些数据,请使用pickle或简单地将数据保存为 JSON 等标准格式。

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.从导入方法你不能简单地传递一个变量作为返回值,你必须在 test_value.py 中指定一个返回函数。 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:在 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.像这样写我有一个功能代码,可以打印从另一个python文件返回的值,就像你问的那样。

hope it helps in the future, but remember that if you want to return a value, you must specify the return function希望对以后有帮助,但是要记住,如果要返回值,必须指定返回函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM