你可以使用 while 循环在 Python 中清除导入文件的内存。

huangapple go评论105阅读模式
英文:

How can I clear the memory of an import file in python with a while loop

问题

I have a while loop, and inside of it, various .py files are imported.

As it is now, when it loops a second time, those .py files are not imported again.

The .py file creates a DataFrame and makes a CSV, but when it loops, it doesn't import the .py file again, so it uses an old CSV.

I've tried deleting the CSV, but the issue is because it doesn't import the file.py in each loop.

Is there a way to make it import the files every time it goes through a loop?

I tried using gc.collect(), but it didn't work.

while True:
    if a == 1:
        import d1makecsv
        df = pd.read_csv(d1)
        os.remove('d1.csv')
    if a == 2:
        import d2makecsv
        df = pd.read_csv(d2)
        os.remove('d2.csv')
    else:
        print("my mom is fat")
df_make_calcs = df
英文:

I have a while loop, and inside of if various import .py

As it is now, when it loops a second time, those import .py files, don't get imported again.

the .py file, creates a df, and makes a csv ... but when it loops, it doesnt import again that .py so it takes an old csv ...
I've tried making delete the csv, but the error is because it doesn't import the file.py in each loop.

Is there some way to make it import the files, each time it makes a loop?

If tried with gc.collect() and it doesn't work.

while (true):
    if a == 1:
        import d1makecsv
        df=pd.read_csv(d1)
        os.remove('d1.csv')
    if a == 2:
        import d2makecsv
        df=pd.read_csv(d2)
        os.remove('d2.csv')
    else:
        print("my mom is fat")
df_make_calcs=df

答案1

得分: 0

尝试这个

import importlib
importlib.reload(YOUR_MODULE)
英文:

Try this

import importlib
importlib.reload(YOUR_MODULE)

huangapple
  • 本文由 发表于 2023年2月26日 19:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定