文件夹判断以及创建操作

#判断文件路径是否存在,如果不存在,则创建,此处是创建多级目录
    if not os.path.isdir(file_dir):

        os.makedirs(file_dir)

判断一个路径是否存在

可以判断一个文件或目录(文件夹)是否存在

    import os.path
    os.path.exists(path);
  • 1
  • 2
  • 3

判断一个文件是否存在

    import os.path
    os.path.isfile(path);
  • 1
  • 2

判断一个目录(文件夹)是否存在

    import os.path
    os.path.isdir(path);

判断一个路径是文件还是目录(文件夹)

  • 方法一
    import os.path
    os.path.isdir(path);
    # 返回 True 表示是目录(文件夹)

  • 方法二
    import os.path
    os.path.isfile(path);
    # 返回 True 表示是文件


猜你喜欢

转载自blog.csdn.net/weixin_42309501/article/details/80858967