准备
1.
被引用的文件:
D:\gogncheng\apiAutoMate\api\a\bePerform.py
2.
执行的文件:
D:\gogncheng\apiAutoMate\common\b\perform.py
适用场景:在
perform.py
下引用文件
bePerform.py
运行代码,分别获取引用文件与执行文件的目
录及名称
bePerform.py
(被引用的文件)
#!/usr/bin/python3
# encoding:utf-8
'''
Created on 2020-06-02 15:24
@author: Administrator
'''
import os
import sys
import time
from test.test_decimal import file
class Fileinfo():
#
获取本文件目录(不管执行位置)
@staticmethod
def getTheCurrentDir():
print('os.path.abspath(os.path.dirname(__file__):
',os.path.abspath(os.path.dirname(__file__)))
print('os.path.split(os.path.realpath(__file__))[0]:
',os.path.split(os.path.realpath(__file__))[0])
#
其他文件引用本文件,调用该方法,获取执行文件的文件目录
@staticmethod
def getPerTheCurrentDir():
print('os.getcwd: ',os.getcwd())
print('sys.path[0]: ',sys.path[0])
#
获取本文件上级目录(不管执行位置)
@staticmethod
def getOnDir():
print('os.path.abspath(os.path.dirname(os.path.dirname(__file__))):
',os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
#
其他文件引用本文件,调用该方法,获取执行文件的文件上级目录
@staticmethod
def getPerOnDir():
print('os.path.abspath(os.path.dirname(os.getcwd())):
',os.path.abspath(os.path.dirname(os.getcwd())))
print('os.path.abspath(os.path.join(os.getcwd(),
".."))',os.path.abspath(os.path.join(os.getcwd(), "..")))
##
获取上上级目录:
os.path.abspath(os.path.join(os.getcwd(), "../.."))
#
获取本文件名称(不管执行位置)
@staticmethod
def getFileName():
print ('os.path.basename(__file__): ',os.path.basename(__file__))
print('os.path.split(os.path.realpath(__file__))[1]:
',os.path.split(os.path.realpath(__file__))[1])