python获取当前文件路径以及父文件路径


1

2

3

4

5

6

#当前文件的路径

pwd = os.getcwd()

#当前文件的父路径

father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")

#当前文件的前两级目录

grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")

 


第一种方法:

os.path.abspath(__file__)

假设app.py中想读取config.ini文件的内容,首先app.py需要知道config.ini的文件路径,从目录结构上可以看出,config.ini与app.py的父目录同级,也就是获取到app.py父目录(bin文件夹的路径)的父目录(config文件夹路径)的绝对路径再拼上config.ini文件名就能获取到config.ini文件

首先,在app.py中测试一下:

import os

def load_file():
# 获取当前文件路径
current_path = os.path.abspath(__file__)
# 获取当前文件的父目录
father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
# config.ini文件路径,获取当前目录的父目录的父目录与congig.ini拼接
config_file_path=os.path.join(os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".."),‘config.ini‘)
print(‘当前目录:‘ + current_path)
print(‘当前父目录:‘ + father_path)
print(‘config.ini路径:‘ + config_file_path)

load_file()

  

输出结果:

当前目录:/Users/shanml/Documents/python/config/bin/app.py
当前父目录:/Users/shanml/Documents/python/config/bin
config.ini路径:/Users/shanml/Documents/python/config/config.ini
从结果中可以看到一切都正常,没有什么问题,假如现在需要从main.py中执行app.py的load_file()方法呢?
来测试一下:

main.py

from bin.app import load_file

if __name__==‘__main__‘:
load_file()

  

输出结果,路径同样没问题:

当前目录:/Users/shanml/Documents/python/config/main.py
当前父目录:/Users/shanml/Documents/python/config
config.ini路径:/Users/shanml/Documents/python/config.ini
参考:https://www.cnblogs.com/yajing-zh/p/6807968.html

第二种方法:
使用inspect

app.py:

import os,inspect

def load_file():
# 获取当前文件路径
current_path=inspect.getfile(inspect.currentframe())
# 获取当前文件所在目录,相当于当前文件的父目录
dir_name=os.path.dirname(current_path)
# 转换为绝对路径
file_abs_path=os.path.abspath(dir_name)
# 划分目录,比如a/b/c划分后变为a/b和c
list_path=os.path.split(file_abs_path)
print(‘list_path:‘ + str(list_path))
# 配置文件路径
config_file_path=os.path.join(list_path[0],‘config.ini‘)
print(‘当前目录:‘ + current_path)
print(‘config.ini文件路径:‘ + config_file_path)

  

在app.py中执行load_file()方法:

list_path:(‘/Users/shanml/Documents/python/config‘, ‘bin‘)
当前目录:/Users/shanml/Documents/python/config/bin/app.py
config.ini文件路径:/Users/shanml/Documents/python/config/config.ini

在mian.py中执行load_file方法:

list_path:(‘/Users/shanml/Documents/python/config‘, ‘bin‘)
当前目录:/Users/shanml/Documents/python/config/bin/app.py
config.ini文件路径:/Users/shanml/Documents/python/config/config.ini

参考:https://xnow.me/programs/python.html
---------------------
作者:S_H-A_N
来源:CSDN
原文:https://blog.csdn.net/lom9357bye/article/details/79285170
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/sddai/p/10408524.html

时间: 2024-08-21 02:25:26

python获取当前文件路径以及父文件路径的相关文章

使用python获取webservice数据并输出到文件

上头要求设置TCP备案检查,给了个WEBSERVICE接口.查了2天,才确认还是python比较好用,我这水平也就写个脚本把数据导出,过滤检索还是用的shell.写此文备忘.WEBSERVICE接口脚本如下: #! /usr/bin/python #coding:utf-8 import codecs import suds def main(file_name, out_file): url = 'http://121.14.4.210:8088/icpautobj/ws/getIcp?wsd

(转)intellij idea svn 修改文件后,父文件夹也标注修改

svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File-->settings-->version control-–>勾选show directories with changed descendants http://blog.csdn.net/wangjun5159/article/details/71250367

python获取知乎日报另存为txt文件

前言 拿来练手的,比较简单(且有bug),欢迎交流~ 功能介绍 抓取当日的知乎日报的内容,并将每篇博文另存为一个txt文件,集中放在一个文件夹下,文件夹名字为当日时间. 使用的库 re,BeautifulSoup,sys,urllib2 注意事项 1.运行环境是Linux,python2.7.x,想在win上使用直接改一下里边的命令就可以了 2.bug是在处理 “如何正确吐槽”的时候只能获取第一个(懒癌发作了) 3.直接获取(如下)内容是不可以的,知乎做了反抓取的处理 urllib2.urlop

Python获取当前文件路径及父文件路径

import os # 当前文件的路径 1.os.getcwd(): 2.os.path.realpath(__file__) # 当前文件的父路径 1.pwd=os.getcwd()   os.path.abspath(os.path.dirname(pwd)+os.path.sep+".") : 2.os.path.dirname(os.path.realpath(__file__)) # 当前文件的前两级目录 1.pwd=os.getcwd()   os.path.abspath

python 获取当前文件夹路径及父级目录的几种方法

获取当前文件夹路径及父级目录: import os current_dir = os.path.abspath(os.path.dirname(__file__)) print(current_dir) #F:\project\pritice current_dir1 = os.path.dirname(__file__) print(current_dir1) #F:/project/pritice parent_path = os.path.dirname(current_dir1) pri

Python获取当前脚本文件夹(Script)的绝对路径

Python获取当前脚本绝对路径 Python脚本有一个毛病,当使用相对路径时,被另一个不同目录下的py文件中导入时,会报找不到对应文件的问题.感觉是当前工作目录变成了导入py文件当前目录.如果你有配置文件的读取操作,然后都放在一个py文件中,而你又用的是相对路径,而且这个py文件在多个不同目录下的py文件中被导入,那就呵呵了...还是用绝对路径吧. 解决这个问题,可以用绝对路径.当然是自动的绝对路径,而不是每次都手动给前缀赋值,让脚本自动寻找当前文件的绝对路径. 此处分享在python下获取一

python获取文件路径

摘自:https://blog.csdn.net/Poo_Chai/article/details/89764001 import os root_path = os.path.abspath(os.path.join(os.getcwd(), "..")) print("""*********************** Path test:start..... ********************""") print(

Python获取目录、文件的注意事项

Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >>> os.listdir(r'E:')['$RECYCLE.BIN', 'BaiduYunDownload', 'cabspottingdata', 'cabspottingdata.tar.gz', 'data', 'MyDownloads', 'System Volume Information'

python获取文件扩展名的方法(转)

主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path):   return os.path.splitext(path)[1] print file_extension('C:\py\wxPython.gif') 输出结果为:.gif 原文地址:https://www.cnblogs.com/hixiaowei/p/8438930.html