Check to see if a file exists, and create it if it doesn't

This question already has an answer here:

  • How to check whether a file exists? 39 answers

  • 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.

    链接地址: http://www.djcxy.com/p/7308.html

    上一篇: 获得参数的完整路径

    下一篇: 检查文件是否存在,如果不存在则创建它