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异步回调机制实现和使用方法
Nov 26 Python
python复制文件代码实现
Dec 23 Python
Python与shell的3种交互方式介绍
Apr 11 Python
浅析Python中的序列化存储的方法
Apr 28 Python
Python闭包的两个注意事项(推荐)
Mar 20 Python
Python实现微信好友的数据分析
Dec 16 Python
python itsdangerous模块的具体使用方法
Feb 17 Python
构建高效的python requests长连接池详解
May 02 Python
Python 如何反方向迭代一个序列
Jul 28 Python
总结Pyinstaller的坑及终极解决方法(小结)
Sep 21 Python
Python爬虫之Selenium实现键盘事件
Dec 04 Python
python实现会员信息管理系统(List)
Mar 18 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
如何开始收听短波广播
2021/03/01 无线电
摩卡咖啡
2021/03/03 咖啡文化
教你如何快捷的使用cmd访问mysql小技巧
2014/05/26 PHP
PHP实现读取文件夹及批量重命名文件操作示例
2019/04/15 PHP
jQuery 动态酷效果实现总结
2009/12/27 Javascript
jQuery Validation插件remote验证方式的Bug解决
2010/07/01 Javascript
关于jQuery的inArray 方法介绍
2011/10/08 Javascript
extjs 时间范围选择自动判断的实现代码
2014/06/24 Javascript
JS实现点击按钮自动增加一个单元格的方法
2015/03/09 Javascript
JavaScript通过join函数连接数组里所有元素的方法
2015/03/20 Javascript
利用jquery禁止外层滚动条的滚动
2017/01/05 Javascript
Bootstrap框架安装使用详解
2017/01/21 Javascript
JavaScript基本语法_动力节点Java学院整理
2017/06/26 Javascript
vue 使用vant插件做tabs切换和无限加载功能的实现
2020/11/04 Javascript
nestjs返回给前端数据格式的封装实现
2021/02/22 Javascript
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
Python正规则表达式学习指南
2016/08/02 Python
python socket网络编程之粘包问题详解
2018/04/28 Python
tensorflow实现简单的卷积网络
2018/05/24 Python
Python实现程序判断季节的代码示例
2019/01/28 Python
python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形)
2019/05/30 Python
Python3+Appium安装使用教程
2019/07/05 Python
利用pandas合并多个excel的方法示例
2019/10/10 Python
python基于gevent实现并发下载器代码实例
2019/11/01 Python
离线状态下在jupyter notebook中使用plotly实例
2020/04/24 Python
完美解决python针对hdfs上传和下载的问题
2020/06/05 Python
python3.4中清屏的处理方法
2020/07/06 Python
英国品牌男装折扣网站:Brown Bag
2018/03/08 全球购物
英国排名第一的礼品体验公司:Red Letter Days
2018/08/16 全球购物
Linux文件系统类型
2012/02/15 面试题
公务员个人自我评价分享
2013/11/06 职场文书
应届生会计求职信
2013/11/11 职场文书
深入开展党的群众路线教育实践活动方案
2014/02/04 职场文书
2014大学校园光棍节活动策划书
2014/09/29 职场文书
房地产工程部经理岗位职责
2015/04/09 职场文书
创业计划书之寿司
2019/07/19 职场文书