• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python判断文件和文件夹是否存在的方法

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python 判断文件夹,python 创建文件夹,python 遍历文件夹,python 删除文件夹,python读取文件夹等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、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
</div>

二、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
</div>

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

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

代码如下:
>>> import os
>>> os.path.exists(r'C:\1.TXT')
False
>>>
</div>
如果存在返回值为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
>>>
</div>

五、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"
</div>
你也可以通过下面的方式实现:
def file_exists(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False
</div>

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

import os
os.path.isfile('test.txt') #如果不存在就返回False
os.path.exists(directory) #如果目录不存在就返回False
</div>

七、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()
</div>
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)
</div>
不能读此文件,也视为不存在。

</div>

您可能想查找下面的文章:

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

相关文章

  • Python爬取网页中的图片(搜狗图片)详解
  • Python切片知识解析
  • 使用C#配合ArcGIS Engine进行地理信息系统开发
  • 利用打码兔和超人打码自封装的打码类分享
  • Python实现删除Android工程中的冗余字符串
  • python的迭代器与生成器实例详解
  • python通过shutil实现快速文件复制的方法
  • Python的迭代器和生成器使用实例
  • Python运用于数据分析的简单教程
  • 各个系统下的Python解释器相关安装方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 在Python的Flask框架中使用日期和时间的教程
    • python 多线程应用介绍
    • python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
    • 常见的在Python中实现单例模式的三种方法
    • 常用python编程模板汇总
    • 在ironpython中利用装饰器执行SQL操作的例子
    • Python文本相似性计算之编辑距离详解
    • 利用matplotlib+numpy绘制多种绘图的方法实例
    • Python语法快速入门指南
    • python条件变量之生产者与消费者操作实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有