Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
如何检查文件是否存在?
方法1:
try:
f = open(filename, "r")
# continue with the file.
except OSError: # open failed
# handle the file open case
方法2:
import os
def file_or_dir_exists(filename):
try:
os.stat(filename)
return True
except OSError:
retrun False
方法3:
import os
def dir_exists(filename):
try:
return (os.stat(filename)[0] & 0x4000) != 0
except OSError:
return False
def file_exists(filename):
try:
return (os.stat(filename)[0] & 0x4000) == 0
except OSError:
return False
方法4:
# Check if path exists.
# Works for relative and absolute path.
def path_exists(path):
parent = "" # parent folder name
name = path # name of file/folder
# Check if file/folder has a parent folder
index = path.rstrip('/').rfind('/')
if index >= 0:
index += 1
parent = path[:index]
name = path[index:]
# Searching with iterator is more efficient if the parent contains lost of files/folders
# return name in uos.listdir(parent)
return any((name == x[0]) for x in uos.ilistdir(parent))
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0091719627380371 seconds