Python判断文件和文件夹是否存在的方法
这篇文章主要介绍了Python判断文件和文件夹是否存在的方法,本文还讲解了判断是否为文件或者目录的方法、os.path.lexist的作用、FTP中判断文件或目录是否存在等内容,需要的朋友可以参考下

一、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判断目录或者文件
1. 判断目录是否存在 'isdir',删除目录时只有该目录为空才可以 'rmdir' import os if(os.path.isdir('D:/Python_workspace/spyder_s ...
- Python判断上传文件类型
在开发上传服务时,经常需要对上传的文件进行过滤. 本文为大家提供了python通过文件头判断文件类型的方法,非常实用. 代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- python判断两个文件是否相同
此方法相当于 Linux 系统下的diff,或者是 git 下的 checkout 官方解释请看: https://docs.python.org/2/library/difflib.html #!/ ...
- 【Python备忘】python判断文件和文件夹是否存在
python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...
- python 判断连个 Path 是否是相同的文件夹
python 判断连个 Path 是否是相同的文件夹 import os os.path.normcase(p1) == os.path.normcase(p2) normcase() 在 windo ...
- python判断文件和文件夹是否存在、没有则创建文件夹
原文出处:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...
- python判断文件和文件夹是否存在、创建文件夹
>>> import os >>> os.path.exists('d:/assist') True >>> os.path.exists('d: ...
- python 判断文件和文件夹是否存在、创建文件夹
原文链接:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...
- Python操作文件、文件夹、字符串
Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...
随机推荐
- 09.Hibernate中的事务与并发
事务1. 什么是事务 * 事务就是逻辑上的一组操作,组成事务的各个执行单元,操作要么全都成功,要么全都失败. * 转账的例子:冠希给美美转钱,扣钱,加钱.两个操作组成了一个事情! 2. 事务的特性 * ...
- Extjs4 的一些语法 持续更新中
一.给GridPanel增加成两行toolbar tbar: { xtype: 'container', layout: 'anchor', defaults: {anchor: '0'}, defa ...
- SQLServer:目录
ylbtech-SQLServer:目录 1.返回顶部 2. 文档返回顶部 · https://docs.microsoft.com/zh-cn/sql/sql-server/sql-server ...
- JAVA开源微信管家平台——JeeWx捷微V3.3版本发布(支持微信公众号,微信企业号,支付窗)
JeeWx捷微V3.3版本紧跟微信小程序更新,在原有多触点版本基础上,引入了更多的新亮点:支持微信公众号.微信企业号.支付宝服务窗等多触点开发:采用微服务框架实现,可插拔可集成,轻量级开发:对小程序的 ...
- 爱上一门语言不需要理由——我的js之路
开始记录js学习:~~~~分享一下你的js学习途径吧 决定学习前端之后,开始接触JavaScript 1995年,网景公司的Brendan Eich用10天完成了JavaScript的设计,他被称为J ...
- C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客
原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...
- InnoDB: Error number 24 means ‘Too many open files’
一.问题的描述 备份程序 执行前滚的时候报错.(-apply-log) InnoDB: Errornumber 24 means 'Too many open files'. InnoDB: Some ...
- Django数据库连接丢失问题
问题 在Django中使用mysql偶尔会出现数据库连接丢失的情况,错误通常有如下两种 1. OperationalError: (2006, 'MySQL server has gone away' ...
- 使用alibaba的json工具将String类型转为JSONArray类型
转化流程:先将输入流转为String类型,再使用alibaba的json转换工具,将字符串转化为json数组 SensorDevices sensorDevices = new SensorDevic ...
- numpy.flatnonzero():
numpy.flatnonzero(): 该函数输入一个矩阵,返回扁平化后矩阵中非零元素的位置(index) 这是官方文档给出的用法,非常正规,输入一个矩阵,返回了其中非零元素的位置. 1 >& ...