python基础之获取文件目录及名称

准备

  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])
    #其他文件引用本文件,调用该方法,获取执行文件的文件名称
    @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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值