2018年1月10日 星期三

Python: Find path refers to a file or directory when you encounter a path name

Python: Find path refers to a file or directory when you encounter a path name 找到到路徑名稱時,查找路徑指的是檔案或目錄

Python Basic: Exercise-108 with Solution

Write a Python program to find path refers to a file or directory when you encounter a path name.
Sample Solution:-
import os.path

for file in [ __file__, os.path.dirname(__file__), '/', './broken_link']:
    print('File        :', file)
    print('Absolute    :', os.path.isabs(file))
    print('Is File?    :', os.path.isfile(file))
    print('Is Dir?     :', os.path.isdir(file))
    print('Is Link?    :', os.path.islink(file))
    print('Exists?     :', os.path.exists(file))
    print('Link Exists?:', os.path.lexists(file))


================= RESTART: F:/Python_APSC/py-ex-basic-108.py =================
File        : F:/Python_APSC/py-ex-basic-108.py
Absolute    : True
Is File?    : True
Is Dir?     : False
Is Link?    : False
Exists?     : True
Link Exists?: True
File        : F:/Python_APSC
Absolute    : True
Is File?    : False
Is Dir?     : True
Is Link?    : False
Exists?     : True
Link Exists?: True
File        : /
Absolute    : True
Is File?    : False
Is Dir?     : True
Is Link?    : False
Exists?     : True
Link Exists?: True
File        : ./broken_link
Absolute    : False
Is File?    : False
Is Dir?     : False
Is Link?    : False
Exists?     : False
Link Exists?: False
>>> 

沒有留言:

張貼留言

MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構

MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 這張圖片展示了一個 結合 MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 (主要透過 Node-RED 進行資料整合)。 系統包含三個核心部分,其運作功能說明如下: 1. ESP32 終端設備(硬體控制層...