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判断文件和文件夹是否存在的方法的更多相关文章

  1. python判断目录或者文件

    1. 判断目录是否存在 'isdir',删除目录时只有该目录为空才可以 'rmdir' import os if(os.path.isdir('D:/Python_workspace/spyder_s ...

  2. Python判断上传文件类型

    在开发上传服务时,经常需要对上传的文件进行过滤. 本文为大家提供了python通过文件头判断文件类型的方法,非常实用. 代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  3. python判断两个文件是否相同

    此方法相当于 Linux 系统下的diff,或者是 git 下的 checkout 官方解释请看: https://docs.python.org/2/library/difflib.html #!/ ...

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

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

  5. python 判断连个 Path 是否是相同的文件夹

    python 判断连个 Path 是否是相同的文件夹 import os os.path.normcase(p1) == os.path.normcase(p2) normcase() 在 windo ...

  6. python判断文件和文件夹是否存在、没有则创建文件夹

    原文出处:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...

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

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

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

    原文链接:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...

  9. Python操作文件、文件夹、字符串

    Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...

随机推荐

  1. 07_springmvc校验

    一.概述 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrol ...

  2. C++给组合框控件(Combo box)加变量后不能运行

    是一个BUG,找出你程序存储的位置,打开一个Debug的文件夹,将除.res文件之外的所有文件删除,然后运行,就可以了!!

  3. 多线程同步锁和死锁以及synchronized与static synchronized 的区别

    线程:线程是进程中的一个执行单元,负责当前进程中程序的执行,一个进程中至少有一个线程.一个进程中是可以有多个线程的,这个应用程序也可以称之为多线程程序.简而言之:一个程序运行后至少有一个进程,一个进程 ...

  4. warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

    解决方法:是所有项目的这个"代码生成"属性设置保持一致. 项目——属性——配置属性——C/C++——代码生成:他有/MT,/MTd,/Md,/MDd四个选项,你必须让所有使用的库都 ...

  5. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  6. 常见的5个runtime exception

    NullPointException(空指针异常),ArrIndexOutOfBoundsException(数组越界异常),ClassCastException(类型转换异常),ClassNotFo ...

  7. python基础---递归函数 知识点自查填空题

    什么是递归函数:在函数中调()叫递归函数. 递归函数最大递归深度是997或998----是()设的限制. 注:如果递归次数太多,就不适合使用递归来解决问题. 递归的缺点: 占(). 递归的优点:会让代 ...

  8. Spring.之.报错:Caused by: java.lang.IllegalArgumentException: No Spring Session store is configured: set the 'spring.session.store-type' property

    Spring.之.报错 No Spring Session store is configured springboot在启动的时候报如下错误: Error starting ApplicationC ...

  9. 11.5 临近csp·道别

    差不多到写这个东西的时候了? 嗯,按今天的日期来算的话,还有不到十天就是csp.感觉对我这种家伙来说应该算是终结了? 放在之前的话肯定会写很多东西的,不过现在大约有点不知道写什么比较合适. 所以只是祝 ...

  10. 2019-4-6-VisualStudio-编码规范工具-2.6-修改当前文件编码

    title author date CreateTime categories VisualStudio 编码规范工具 2.6 修改当前文件编码 lindexi 2019-04-06 15:31:53 ...