我正在写一个程序,按顺序执行一组任务。每个任务都是一个输出新文件的函数,但是如果文件名已经存在,则不应执行任何给定的任务。我发现自己一遍又一遍地写着这样的代码:
task1_fname = task1()
# come up with filename for next task
task2_fname = "task2.txt"
if not os.path.isfile(task2_fname):
# task2 might depend on task1's output, so it gets task1's filename
task2(task1_fname)
task3_fname = "task3.txt"
if not os.path.isfile(task3_fname):
task3(...)
基本思想是,如果文件存在(理想情况下为非空),则不应执行生成此文件的任务。你知道吗
不必每次都写os.path.isfile
调用,最好的Pythonic方式是什么?装饰师能更简洁地表达这一点吗?或者类似于:
with task2(task1_fname):
# continue to next task
有什么想法吗?你知道吗
你在找这样的东西吗?你知道吗
相关问题 更多 >
编程相关推荐