Check to see if a file exists, and create it if it doesn't
This question already has an answer here:
You can use os.path.exists("schedule.dat")
. It returns a boolean result.
An alternative solution using os.stat
involves:
import os
try:
os.stat("schedule.dat")
... # file exists
except:
file = open("schedule.dat", "w")
...
An exception is raised is you try to stat
a non-existent file.
上一篇: 获得参数的完整路径
下一篇: 检查文件是否存在,如果不存在则创建它