Python判断文件和文件夹是否存在的方法
2023-09-14 09:00:14 时间
这篇文章主要介绍了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):
else:
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
[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
PATH='./file.txt'
if path.exists(PATH) and
path.isfile(PATH) and access(PATH, R_OK):
else:
你也可以通过下面的方式实现:
代码如下:
def file_exists(filename):
六、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()
>>> 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)
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】python 多线程两种实现方式
- python实现在目录中查找指定文件的方法
- python读写文件数据:with读、写文件
- 在Python中操作文件之truncate()方法的使用教程
- Python导出DBF文件到Excel的方法
- Python 日期和时间_python 当前日期时间_python日期格式化
- python 编辑文件时路径问题解决方法:文件或者目录不存在、文件编辑后无法保存等(以编辑xml文件为例)
- paip.判断文件是否存在uapi python php java c#
- paip.文件读写api php java python总结.txt
- Python:利用for循环比较两个列表元素的异同进而找出共有元素、各自不同元素并全部导出到csv文件实现代码
- Python之ffmpeg-python:ffmpeg-python库的简介、安装、使用方法之详细攻略
- Python编程语言学习:利用open函数将文本内容追加写入到txt文件中(两种方法实现)
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- Python语言学习之图表可视化:python语言中可视化工具包的简介、安装、使用方法、经典案例之详细攻略
- Python之ffmpeg-python:ffmpeg-python库的简介、安装、使用方法之详细攻略
- 〖Python 数据库开发实战 - Python与MySQL交互篇⑩〗- 创建新闻管理系统的具体python文件
- python基础知识之 Python代码规范
- Python Flask框架学习30:redis操作/配置静态文件路径
- Python极简方法生成Zip文件
- python+appium+pytest自动化测试-yaml文件配置Capabilities
- 多版本Python共存时pip给指定版本的python安装package的方法
- 〖Python自动化办公篇⑪〗- word文件自动化 - word 转 PDF(pdfkit与pydocx相结合)
- python工具方法 17 labelme标注的json文件转voc数据再转训练列表
- 【Python】1.python 删除文件夹和文件
- Python .py 文件打包成 .exe 文件(Windows平台,python 3.x)