# __file__是当前执行的文件
# 获取当前文件__file__的路径
os.path.realpath(__file__)
os.path.abspath(__file__)
# 获取当前文件__file__的所在目录
os.path.dirname(os.path.realpath(__file__))
ubuntu@ThinkCentre:/home/public/tmp$ tree
.
├── txtfile
└── txtfile_ln -> /home/public/tmp/txtfile
0 directories, 2 files
ubuntu@ThinkCentre:/home/public/tmp$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.realpath('txtfile_ln')
'/home/public/tmp/txtfile'
>>> os.path.abspath('txtfile_ln')
'/home/public/tmp/txtfile_ln'
>>> os.path.abspath('txtfile')
'/home/public/tmp/txtfile'
>>> os.path.realpath('txtfile')
'/home/public/tmp/txtfile'
import errno
import os
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise