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
>>> 

沒有留言:

張貼留言

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE   const char broker[] = "test.mosquitto.org" ; //const char broker[] = "br...