我有一个脚本,它对所有DB‘运行一些检查,现在我希望有一个列表,以便这个列表包含所有DB的checked.So,下一次脚本运行时,它将读取这个列表,如果DB不在该列表中,那么检查就会发生。实现这一点的最佳方法是什么?如果我初始化一个空列表(DB_checked)并在运行检查时追加每个DB名称,那么问题是每次脚本启动时,该列表将再次为空。请suggest.Thanks。
在脚本的末尾,将调用以下函数将其写入磁盘:
def writeDBList(db_checked):
with open(Path(__file__).parent / "db_names.txt", "w") as fp:
for s in job_names:
fp.write(str(s) +"\n")
return当脚本启动时,将调用下面的命令从磁盘读取文件:
def readDBList():
with open(Path(__file__).parent / "db_names.txt", "r") as fp:
for line in fp:
db_list.append(line.strip())
return但是,如何将文件内容转换为列表,以便我可以轻松地查看以下内容:
checked_list = readDBList()
if db not in checked_list:
....
....发布于 2020-01-28 13:26:02
您需要在脚本完成检查后将此列表写入磁盘,并在下一个脚本运行时在脚本的开头再次读取它。
# Read DB CheckList
DB_List = readDBList()
# Your normal script functionality for only DBs not in the list
# Store DB CheckList
writeDBList(DB_List) 如果您不熟悉python中的I/O文件处理,请检查这。
现在,关于你的第二个问题,关于如何阅读清单。我建议使用泡菜,它允许您读/写python结构,而不必担心字符串或解析。
import pickle
def writeDBList():
with open('DBListFile', 'wb') as fp:
pickle.dump(DBList, fp)
def readDBList():
with open ('DBListFile', 'rb') as fp:
DBList= pickle.load(fp)https://stackoverflow.com/questions/59949670
复制相似问题