使用 os 模块

  • 判断文件是否存在

    os.path.isfile(path)
  • 判断目录是否存在

    os.path.isdir(path)
  • 判断路径是否存在

    # 使用 path 模块
    os.path.exists(path) # 使用 access() 方法
    os.access(path, os.F_OK)

使用 open 函数和异常捕获

如果直接用 open() 函数打开一个不存在的文件时,程序会抛出异常,我们可以通过 try 语句来捕获异常以达到判断文件是否存在的目的。

如果文件不存在,open() 函数会抛出 FileNotFoundError 异常。如果文件无操作权限,则会抛出 PersmissionError 异常。

filePath = '/path/to/file'

try:
file = open(filePath)
file.close()
except FileNotFoundError:
print("No such file or directory: '%s'" % filePath)
except IsADirectoryError:
print("Is a directory: '%s'" % filePath)
except PermissionError:
print("Permission denied: '%s'" % filePath)
else:
print("File is exist: '%s'" % filePath)

使用 pathlib 模块

import pathlib

path = pathlib.Path('path/to/file')

# 判断路径是否存在
path.exists() # 判断是否为文件
path.is_file() # 判断是否为目录
path.is_dir()

个人博客同步地址:
https://shockerli.net/post/python-determine-file-exist/

Python 判断文件/目录是否存在的更多相关文章

  1. 【Python备忘】python判断文件和文件夹是否存在

    python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...

  2. Python判断文件和文件夹是否存在的方法

    Python判断文件和文件夹是否存在的方法 这篇文章主要介绍了Python判断文件和文件夹是否存在的方法,本文还讲解了判断是否为文件或者目录的方法.os.path.lexist的作用.FTP中判断文件 ...

  3. Shell中判断文件,目录是否存在

    一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...

  4. Python OS 文件/目录方法

    Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...

  5. shell判断文件,目录是否存在或者具有权限

    shell判断文件,目录是否存在或者具有权限  #!/bin/sh  myPath="/var/log/httpd/"  myFile="/var /log/httpd/ ...

  6. QT判断文件/目录是否存在

    最近在用qt写一个ui,遇到删除sd卡中的文件失败情况,有些时候是存在删除链表里面的文件在sd卡上已经不存在了,导致失败,以为我的链表是定时刷新的,但是文件是实时更新会同步覆盖的.这样就存在可能上一秒 ...

  7. python 判断文件是否存在和删除文件的api (其中判断文件在不在让想起这个可以强兼容jenkins工作目录那个问题)

    判断文件在不在的api: os即operating system(操作系统),Python 的 os 模块封装了常见的文件和目录操作. os.path模块主要用于文件的属性获取,exists是“存在” ...

  8. Python 判断文件是否存在的三种方法

    通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. 这里将介绍三种判断文件或文件夹是否存在的方法,分别使用os模块.Try ...

  9. php 判断文件/目录是否存的方法

    涉及函数 is_file(), is_dir() , file_exists() is_file() 判断文件是否存在 is_dir() 判断目录是否存在 file_exists() 既可用于判断文件 ...

随机推荐

  1. 关闭浏览器时提示的javascript事件

    onbeforeunload事件 它是这样用的: <script language="javascript"> g_blnCheckUnload = true; fun ...

  2. boost asio 学习(九) boost::asio 网络封装

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...

  3. JS基础-数组的常用方法-冒泡排序

    1.数组  1.关联数组    以数字作为元素下标的数组,就是索引数组.    以字符串作为元素下标的数组,就是关联数组.  2.js的关联数组    ex:在php中       $array=[& ...

  4. python发送邮箱

    一.Python发送邮件 import smtplib from email.mime.text import MIMEText def send(email_to,title,content): m ...

  5. ABP框架系列之三十:(Javascript-API-Javascript-API)

    ASP.NET Boilerplate provides a set of objects and functions that are used to make javascript develop ...

  6. HDU 2639 01背包(分解)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...

  7. CentOS6最佳实践

    一  安装常用软件 常用目录结构 源文件目录  /application,原包文件及解压文件 如  /application/Python-3.6.0.tgz 软件配置目录 /usr/local/ 如 ...

  8. 在Git中设置自己的姓名

    在Git中,自己的姓名与每一个commit提交绑定在一起.如果你在使用Azure DevOps Server中的Git Repo时,一定要注意commit中的提交者与服务器上的推送者,是两个概念. 在 ...

  9. SpringDataSolr 过滤(或者叫筛选)查询

    // 被本类调用 private Map searchList(Map searchMap) { // 1.1关键字查询 SimpleHighlightQuery highlightQuery = n ...

  10. linux 环境安装

    lnmp.lamp.lnmpa一键安装包(Updated: 2019-02-17) 422 A+ 所属分类:工具 这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/lamp/lnmpa( ...