方法一:
import sys,os
os.getcwd()#然後就可以看見結果了
方法二:
import os
os.path.dirname(os.path.realpath('__file__'))#注意:添加單引號
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
那麼將輸出空字符串
python中的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:\