2018年1月10日 星期三

Python: Divide a path on the extension separator

Python: Divide a path on the extension separator

Python Basic: Exercise-106 with Solution

Write a Python program to divide a path on the extension separator.
Sample Solution:-
import os.path
for path in [ 'test.txt', 'filename', 'F:\Python_APSC/test.txt', '/', '' ]:
    print('"%s" :' % path, os.path.splitext(path))

 split(path):將路徑分解為(文件夾,文件名) 
 splitext(path):將路徑分解為(其余部分,.擴展名),若文件名中沒有擴展名,擴展名部分為空字符串

=========== RESTART: F:/Python_APSC/py-ex-basic-106.py ==========
"test.txt" : ('test', '.txt')
"filename" : ('filename', '')
"F:\Python_APSC/test.txt" : ('F:\\Python_APSC/test', '.txt')
"/" : ('/', '')
"" : ('', '')
>>> 

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...