python中的os.path.dirname的用法
os.path.dirname(path)
語法:os.path.dirname(path)
功能:去掉文件名,返回目錄
如:
print(os.path.dirname('W:\Python_File\juan之購物車.py')) #結果 #W:\Python_File
print(os.path.dirname('W:\Python_File')) #結果 #W:\
python中的os.path.dirname(__file__)的使用
(1).當"print os.path.dirname(__file__)"所在腳本是以完整路徑被運行的, 那么將輸出該腳本所在的完整路徑,比如:
python d:/pythonSrc/test/test.py
那么將輸出 d:/pythonSrc/test
(2).當"print os.path.dirname(__file__)"所在腳本是以相對路徑被運行的, 那么將輸出空目錄,比如:
python test.py
那么將輸出空字符串