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

沒有留言:

張貼留言

RFID TI 培訓影片系列

RFID TI 培訓影片系列  https://www.ti.com/zh-tw/video/series/rfid.html 培訓影片系列 RFID 隨著創新技術日益發展,RFID 和 RF 術語越來越容易讓人混淆。本訓練系列詳細介紹了使用案例、權衡技術優缺點,讓您清楚知道該選...