我需要在不同的数据集上重复运行一个python脚本。python脚本test.py使用命令处理数据集、打印和保存结果。
plt.savefig('result.png')
如何确保相同的test.py
脚本在另一个数据集上运行。。新的result.png
不会覆盖我以前的结果吗?基本上,在执行plt.savefig('result.png')
之前,我需要检查result.png是否已经存在,如果已经存在,则将结果重命名为任何其他名称,如
result1.png
result2.png
否则,在下一个后期处理中,文件将被覆盖。
Tags:
您可以使用标准库中的
tempfile.mkstemp
注意:
fi
是一个整数。如果你需要一个文件对象而
filename
包含所创建文件的绝对路径名,例如另一个解决方案是使用uuid,例如
会产生一些
更多信息和参数请参见官方文件 在mkstemp和uuid上
您可以使用
os.path.exists
检查文件是否已经存在,如果已经存在,则追加一个数字。重复使用新文件名,直到找到一个尚不存在的文件名。示例用法:
相关问题 更多 >
编程相关推荐