Python to determine whether a file or directory exists method full!

Python to determine whether a file or directory exists method full!

1:
import os.path
isFileExist(filePath):
    return os.path.isfile(filePath)

2:
from pathlib import Path
isFileExist(filePath):
    return Path(filePath).is_file()
    
isDirExist(filePath):
    return Path(filePath).is_dir()

3:
from pathlib import Path
isFileExist(filePath):
    return Path(filePath).exists()

4:
from pathlib import Path
isFileExist(filePath):
    try:
        absPath = Path(filePath).resolve()
        except FileNotFoundError:
            return false
        else:
            return True

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example