Python判断文件和文件夹是否存在的方法
作者:junjie 时间:2023-09-12 17:22:00
一、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,判断,文件,文件夹,是否存在
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php5.4以下版本json不支持不转义内容中文的解决方法
2023-07-02 17:10:45
vue2.0父子组件间通信的实现方法
2024-04-28 09:20:16
python检测文件夹变化,并拷贝有更新的文件到对应目录的方法
2023-11-07 12:56:06
Python安装OpenCV的示例代码
2022-05-25 23:35:44
![](https://img.aspxhome.com/file/2023/2/134492_0s.jpg)
MySQL安装失败的原因及解决步骤
2024-01-17 18:22:27
![](https://img.aspxhome.com/file/2023/5/69835_0s.jpg)
python opencv摄像头的简单应用
2023-01-17 14:25:56
![](https://img.aspxhome.com/file/2023/3/131383_0s.jpg)
Golang如何实现任意进制转换的方法示例
2024-02-23 07:43:48
MySQL数据库安装和Navicat for MySQL配合使用教程
2024-01-24 16:50:58
![](https://img.aspxhome.com/file/2023/3/115553_0s.png)
linux中用shell快速安装配置Go语言的开发环境
2024-02-12 22:38:11
MySQL中KEY、PRIMARY KEY、UNIQUE KEY、INDEX 的区别
2024-01-16 00:47:29
xhtml+css页面制作过程中问题的解决方案
2008-08-05 18:00:00
防止MySQL注入或HTML表单滥用的PHP程序
2024-05-11 10:12:49
浅析python打包工具distutils、setuptools
2021-03-30 14:45:14
![](https://img.aspxhome.com/file/2023/2/65132_0s.png)
Python中利用aiohttp制作异步爬虫及简单应用
2023-07-21 04:50:40
![](https://img.aspxhome.com/file/2023/6/135286_0s.png)
js弹出的对话窗口永远保持居中显示
2024-04-23 09:13:00
Python Timer和TimerFPS计时工具类
2022-06-13 12:48:38
![](https://img.aspxhome.com/file/2023/4/105314_0s.png)
Python json模块与jsonpath模块区别详解
2023-09-17 18:07:26
PHP date()格式MySQL中插入datetime方法
2024-05-13 09:51:39
ASP 自动采集实现代码
2011-03-07 11:17:00
oracle数据库下统计专营店的男女数量的语句
2012-07-11 16:01:17