准备
- 被引用的文件:D:\gogncheng\apiAutoMate\api\a\bePerform.py
- 执行的文件: 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])
#其他文件引用本文件,调用该方法,获取执行文件的文件名称
@staticmethod
def getPerFileName():
print ('os.path.basename(sys.argv[0]: ',os.path.basename(sys.argv[0]))
#获取本文件目录名称(不管执行位置)
@staticmethod
def getFileDirName():
fileDir = os.path.split(os.path.realpath(__file__))[0]
fileName = os.path.split(os.path.realpath(__file__))[1]
fileDirName = os.path.join(fileDir,fileName)
print(fileDirName)
#其他文件引用本文件,调用该方法,获取执行文件的文件目录名称
@staticmethod
def getperFileDirName():
print ('sys.argv[0]: ',sys.argv[0])
if __name__ == "__main__":
print("------------TheCurrentDir--------------")
Fileinfo.getTheCurrentDir()
print("------------PerTheCurrentDir--------------")
Fileinfo.getPerTheCurrentDir()
print("----------OnDir----------------")
Fileinfo.getOnDir()
print("----------PerOnDir----------------")
Fileinfo.getPerOnDir()
print("----------FileName----------------")
Fileinfo.getFileName()
print("----------PerFileName----------------")
Fileinfo.getPerFileName()
print("----------FileDirName----------------")
Fileinfo.getFileDirName()
print("-----------perFileDirName---------------")
Fileinfo.getperFileDirName()
perform.py(执行文件)
#!/usr/bin/python3
# encoding:utf-8
'''
Created on 2020-05-24 13:35
@author: Administrator
'''
from api.a.bePerform import Fileinfo
if __name__ == '__main__':
print("------------TheCurrentDir--------------")
Fileinfo.getTheCurrentDir()
print("------------PerTheCurrentDir--------------")
Fileinfo.getPerTheCurrentDir()
print("----------OnDir----------------")
Fileinfo.getOnDir()
print("----------PerOnDir----------------")
Fileinfo.getPerOnDir()
print("----------FileName----------------")
Fileinfo.getFileName()
print("----------PerFileName----------------")
Fileinfo.getPerFileName()
print("----------FileDirName----------------")
Fileinfo.getFileDirName()
print("-----------perFileDirName---------------")
Fileinfo.getperFileDirName()
执行结果
------------TheCurrentDir--------------
os.path.abspath(os.path.dirname(__file__): D:\gogncheng\apiAutoMate\api\a
os.path.split(os.path.realpath(__file__))[0]: D:\gogncheng\apiAutoMate\api\a
------------PerTheCurrentDir--------------
os.getcwd: D:\gogncheng\apiAutoMate\common\b
sys.path[0]: D:\gogncheng\apiAutoMate\common\b
----------OnDir----------------
os.path.abspath(os.path.dirname(os.path.dirname(__file__))): D:\gogncheng\apiAutoMate\api
----------PerOnDir----------------
os.path.abspath(os.path.dirname(os.getcwd())): D:\gogncheng\apiAutoMate\common
os.path.abspath(os.path.join(os.getcwd(), "..")) D:\gogncheng\apiAutoMate\common
----------FileName----------------
os.path.basename(__file__): bePerform.py
os.path.split(os.path.realpath(__file__))[1]: bePerform.py
----------PerFileName----------------
os.path.basename(sys.argv[0]: perform.py
----------FileDirName----------------
D:\gogncheng\apiAutoMate\api\a\bePerform.py
-----------perFileDirName---------------
sys.argv[0]: D:\gogncheng\apiAutoMate\common\b\perform.py