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


Posted in Python onMay 21, 2015

一、python判断文件和文件夹是否存在、创建文件夹

>>> import os

>>> os.path.exists('d:/assist')

True

>>> os.path.exists('d:/assist/getTeacherList.py')

True

>>> os.path.isfile('d:/assist')

False

>>> os.path.isfile('d:/assist/getTeacherList.py')

True

>>> os.makedirs('d:/assist/set')

>>> os.path.exists('d:/assist/set')

True

二、python判断文件是否存在

import os

 

filename = r'/home/tim/workspace/test.txt'

if os.path.exists(filename):

    message = 'OK, the "%s" file exists.'

else:

    message = "Sorry, I cannot find the "%s" file."

print message % filename

三、如何用Python判断文件是否存在

使用os.path.exists()方法可以直接判断文件是否存在。

代码如下:

>>> import os

>>> os.path.exists(r'C:\1.TXT')

False

>>>

如果存在返回值为True,如果不存在则返回False

四、python判断文件夹是否存在

$ python

Python 2.7.3 (default, Jan  2 2013, 16:53:07) 

[GCC 4.7.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> 

>>> 

>>> tobecheckdir = r'/home/tim/workspace'

>>> os.path.isdir(tobecheckdir)

True

>>>

五、python检查文件是否存在,以及路径是否为文件

在写文件之前通常需要检查文件路径是否可写:

from os import path, access, R_OK  # W_OK for write permission.
PATH='./file.txt'
if path.exists(PATH) and path.isfile(PATH) and access(PATH, R_OK):

    print "File exists and is readable"

else:

    print "Either file is missing or is not readable"

你也可以通过下面的方式实现:
def file_exists(filename):

    try:

        with open(filename) as f:

            return True

    except IOError:

        return False

六、python判断文件和文件夹是否存在

import os 

os.path.isfile('test.txt') #如果不存在就返回False 

os.path.exists(directory) #如果目录不存在就返回False

七、os.path.lexist

还有os.path.lexists(path)
对broken的link file也返回True.

八、python FTP判断文件夹是否存在

python怎样判断文件夹是否存在?广大网友给出了答案:
使用ftp库就可以了,下面是Python核心编程上的例子:

>>> from ftplib import FTP

>>> f = FTP('ftp.python.org')

>>> f.login('anonymous', 'guido@python.org')

'230 Guest login ok, access restrictions apply.'

>>> f.dir()

dir结果中无此文件,就是不存在。
或者如下:
try:

f.retrbinary('RETR %s' % FILE,open(FILE, 'wb').write)

except ftplib.error_perm:

print 'ERROR: cannot read file "%s"' % FILE 40 os.unlink(FILE)

不能读此文件,也视为不存在。
Python 相关文章推荐
python生成IP段的方法
Jul 07 Python
栈和队列数据结构的基本概念及其相关的Python实现
Aug 24 Python
Python的Flask框架应用调用Redis队列数据的方法
Jun 06 Python
深入理解Python中装饰器的用法
Jun 28 Python
Python 详解基本语法_函数_返回值
Jan 22 Python
使用python3+xlrd解析Excel的实例
May 04 Python
pyqt5 使用label控件实时显示时间的实例
Jun 14 Python
Django REST框架创建一个简单的Api实例讲解
Nov 05 Python
使用matplotlib绘制图例标签中带有公式的图
Dec 13 Python
python 比较2张图片的相似度的方法示例
Dec 18 Python
浅谈keras中Dropout在预测过程中是否仍要起作用
Jul 09 Python
如何利用Python给自己的头像加一个小国旗(小月饼)
Oct 02 Python
python使用wxpython开发简单记事本的方法
May 20 #Python
Python使用shelve模块实现简单数据存储的方法
May 20 #Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
May 20 #Python
python获取指定目录下所有文件名列表的方法
May 20 #Python
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
May 20 #Python
Python使用matplotlib绘制动画的方法
May 20 #Python
Python中subprocess模块用法实例详解
May 20 #Python
You might like
PHP学习 运算符与运算符优先级
2008/06/15 PHP
tp5 实现列表数据根据状态排序
2019/10/18 PHP
JS关键字球状旋转效果的实例代码
2013/11/29 Javascript
基于promise.js实现nodejs的promises库
2014/07/06 NodeJs
使用时间戳解决ie缓存的问题
2014/08/20 Javascript
在Node.js应用中使用Redis的方法简介
2015/06/24 Javascript
JavaScript事件处理的方式(三种)
2016/04/26 Javascript
在js里怎么实现Xcode里的callFuncN方法(详解)
2016/11/05 Javascript
js与jquery分别实现tab标签页功能的方法
2016/11/18 Javascript
js实现购物车功能
2018/06/12 Javascript
Vue创建头部组件示例代码详解
2018/10/23 Javascript
在Vant的基础上实现添加表单验证框架的方法示例
2018/12/05 Javascript
JavaScript模板引擎实现原理实例详解
2018/12/14 Javascript
如何利用 JS 脚本实现网页全自动秒杀抢购功能
2020/10/12 Javascript
原生js实现照片墙效果
2020/10/13 Javascript
[54:41]2018DOTA2亚洲邀请赛3月30日 小组赛B组 VGJ.T VS paiN
2018/03/31 DOTA
python学习手册中的python多态示例代码
2014/01/21 Python
Python 获取新浪微博的最新公共微博实例分享
2014/07/03 Python
Python 实现数据库(SQL)更新脚本的生成方法
2017/07/09 Python
机器学习python实战之手写数字识别
2017/11/01 Python
python多线程并发让两个LED同时亮的方法
2019/02/18 Python
Pytorch 实现自定义参数层的例子
2019/08/17 Python
详解python中的生成器、迭代器、闭包、装饰器
2019/08/22 Python
python数据预处理 :样本分布不均的解决(过采样和欠采样)
2020/02/29 Python
python中not、and和or的优先级与详细用法介绍
2020/11/03 Python
eBay澳大利亚站:eBay.com.au
2018/02/02 全球购物
中学教师实习自我鉴定
2013/09/28 职场文书
九年级科学教学反思
2014/01/29 职场文书
2014年公司庆元旦活动方案
2014/03/05 职场文书
一帮一活动总结
2014/05/08 职场文书
地震捐款倡议书
2014/08/29 职场文书
党支部反对四风思想汇报
2014/10/10 职场文书
2015毕业生简历自我评价
2015/03/02 职场文书
会计专业求职信范文
2015/03/19 职场文书
车辆管理制度范本
2015/08/05 职场文书
导游词之桂林
2019/08/20 职场文书