您当前的位置: 首页 >  Python

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python判断文件是否存在、访问

柳鲲鹏 发布时间:2019-02-14 09:17:51 ,浏览量:0

  • 文件、文件夹是否存在
if (not os.path.exists(file)):
    print("file not exists")
  • 是否能访问
os.access(path, mode)

mode:
os.F_OK: 检查文件是否存在
os.R_OK: 检查文件是否可读
os.W_OK: 检查文件是否可以写入
os.X_OK: 检查文件是否可以执行
  • open
try:
    f = open(file_path, "wb")
    f.close()
except FileNotFoundError:
    print "File is not found."
except PersmissionError:
    print "You don't have permission to access this file."

 

关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.0580s