国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python判斷文件和文件夾是否存在的方法

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

2020-07-04 08:44腳本之家 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)


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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 在线免费观看h片 | 国内外精品一区二区三区 | 欧美freesex交免费视频 | 国产乱码精品1区2区3区 | av一区二区三区四区 | 欧美高清成人 | 午夜视频免费 | 亚洲精品视频在线免费播放 | 一区二区av在线 | 久久免费精品 | 美女视频黄色片 | 青青草综合| 最新国产视频 | 91精品国产综合久久久久久丝袜 | 夜夜超碰 | 日韩精品免费一区二区三区 | 亚洲国产精品电影在线观看 | 国产精品视频入口 | 亚洲h | 阿v视频在线 | 久久草视频 | 久久久www | 久久久婷婷 | 黄色成人在线视频 | 亚洲视频一区在线 | 欧美视频在线播放 | 亚洲成人久久久 | 国产精品久久久久久久久久久久冷 | 中文字幕91 | 人人天天色 | 国内外成人激情免费视频 | 亚洲三区在线观看 | 亚洲精品久久久久久国 | 亚洲成av| 艹逼逼视频 | 99中文字幕 | 99久久国产免费 | 久久手机免费视频 | 噜噜噜噜狠狠狠7777视频 | 君岛美绪一区二区三区在线视频 | 精品国产视频 |