>>importos>>>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.maked" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python判斷文件和文件夾是否存在的方法

系統(tǒng) 1696 0

一、python判斷文件和文件夾是否存在、創(chuàng)建文件夾

復(fù)制代碼 代碼如下:

>>> 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判斷文件是否存在

復(fù)制代碼 代碼如下:

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()方法可以直接判斷文件是否存在。

代碼如下:

復(fù)制代碼 代碼如下:

>>> import os
>>> os.path.exists(r'C:\1.TXT')
False
>>>

如果存在返回值為True,如果不存在則返回False

四、python判斷文件夾是否存在

復(fù)制代碼 代碼如下:

$ 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檢查文件是否存在,以及路徑是否為文件

在寫文件之前通常需要檢查文件路徑是否可寫:

復(fù)制代碼 代碼如下:

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"


你也可以通過下面的方式實現(xiàn):
復(fù)制代碼 代碼如下:

def file_exists(filename):
??? try:
??????? with open(filename) as f:
??????????? return True
??? except IOError:
??????? return False

六、python判斷文件和文件夾是否存在

復(fù)制代碼 代碼如下:

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怎樣判斷文件夾是否存在?廣大網(wǎng)友給出了答案:
使用ftp庫就可以了,下面是Python核心編程上的例子:

復(fù)制代碼 代碼如下:

>>> 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結(jié)果中無此文件,就是不存在。
或者如下:
復(fù)制代碼 代碼如下:

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)

不能讀此文件,也視為不存在。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 崇明县| 沾化县| 颍上县| 三原县| 鹿邑县| 仪陇县| 东乌| 宁国市| 盱眙县| 梁山县| 信阳市| 上杭县| 津市市| 衡阳市| 黄山市| 辽宁省| 昭平县| 黑龙江省| 河东区| 东光县| 巫山县| 汽车| 庆阳市| 罗源县| 剑川县| 政和县| 莱芜市| 林州市| 交城县| 枣阳市| 田东县| 准格尔旗| 泰来县| 宣武区| 静安区| 安远县| 嘉定区| 内丘县| 五台县| 四会市| 鹤庆县|