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. Python学习day15-函数进阶(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  2. .NET Framework的属性类对控件的支持功能

     ToolBoxItem 此属性为类特性.属于工具箱属性,可以设置当前控件是否在工具箱中显示,以及所在工具箱项的类型名称等信息.默认生成的控件都显示在工具箱中. 更多设计时属性介绍: 4.3 属性的 ...

  3. 设置listContrl中指定行的颜色

    在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...

  4. hbase master一直报启动不起来问题(region空洞和region卡在spilt)

    数据不重要或者一直卡着的情况下,可以切换hdfs用户到hbase的wal目录下对spilting的数据进行重命名.具体步骤如下 1.关闭hbase集群 2.切换hdfs用户 3.到hbasewal目录 ...

  5. C开发系列-continue与break

    break break使用场景 switch语句:退出整个switch语句 循环结构:退出整个循环语句 while循环 do while循环 for 循环 continue continue使用场景 ...

  6. Vue跳转相同路由不同参数,解决页面数据不自动刷新

    参考: https://www.cnblogs.com/ainyi/p/9340311.html https://blog.csdn.net/weixin_41888813/article/detai ...

  7. 05.Hibernate常用的接口和类---Configuration类和作用

    Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = n ...

  8. 如何使用Tunnel SDK上传/下载MaxCompute复杂类型数据

    基于Tunnel SDK如何上传复杂类型数据到MaxCompute?首先介绍一下MaxCompute复杂数据类型: 复杂数据类型 MaxCompute采用基于ODPS2.0的SQL引擎,丰富了对复杂数 ...

  9. Angular本地数据存储LocalStorage

    //本地存储数据===================================.factory('locals',['$window',function($window){ return{ / ...

  10. ECS应用管理最佳实践

    前言 即使在CloudNative发展如火如荼的当下,ECS应用(直接将应用部署在ECS上,不使用容器)仍然占了相当大的比重,原因主要在于相对容器化应用,ECS应用由于不需要容器的运行时环境和类似K8 ...