python中的os.path.dirname與os.path.dirname(__file__)的用法

python中的os.path.dirname的用法python

os.path.dirname(path)

 

語法:os.path.dirname(path) 
功能:去掉文件名,返回目錄 
如:spa

 

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__)的使用code

(1).當"print os.path.dirname(__file__)"所在腳本是以完整路徑被運行的, 那麼將輸出該腳本所在的完整路徑,好比:blog

             python d:/pythonSrc/test/test.py字符串

             那麼將輸出 d:/pythonSrc/testclass

     (2).當"print os.path.dirname(__file__)"所在腳本是以相對路徑被運行的, 那麼將輸出空目錄,好比:test

             python test.pyfile

             那麼將輸出空字符串語法

相關文章
相關標籤/搜索