测试文件的名称

path_test.py

先确定文件目录

(my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py

实验运行代码和结果(所有测试在Ubuntu16.04,pycharm2016中运行)

import os
file_path = os.path.abspath(__file__) # 返回的是完整的路径(有文件名)
file_abspath = os.path.dirname(__file__) # 返回的是命令行中的路径(没有文件名)
print(file_path)
print(file_abspath)
print(__file__)
# /home/python/Desktop/flask_news_pro/path_test.py 直接运行
# /home/python/Desktop/flask_news_pro
# /home/python/Desktop/flask_news_pro/path_test.py # (my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py 命令运行 没有路径
# /home/python/Desktop/flask_news_pro/path_test.py
#
#path_test.py # (my_flask3) python@ubuntu:~/Desktop$ python flask_news_pro/path_test.py 命令运行,加路径
# /home/python/Desktop/flask_news_pro/path_test.py
# flask_news_pro
# flask_news_pro/path_test.py file_abspath = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.abspath(os.path.dirname(__file__)) print(file_abspath)
print(file_path) # /home/python/Desktop/flask_news_pro 命令运行 没有路径
# /home/python/Desktop/flask_news_pro # /home/python/Desktop/flask_news_pro 直接运行
# /home/python/Desktop/flask_news_pro # /home/python/Desktop/flask_news_pro 命令运行,加路径
# /home/python/Desktop/flask_news_pro

实例:

def setup_log(config_name):
"""配置日志"""
dir_file = os.path.abspath(__file__)
dir_info = os.path.dirname(dir_file) # 返回上一级目录
dir_base = os.path.dirname(dir_info)
dir_log = os.path.join(dir_base, 'logs/log')
print(dir_file)
print(dir_info)
print(dir_base)
print(dir_log)

  

/home/python/Desktop/flask_news_pro/xjzx/info/__init__.py
/home/python/Desktop/flask_news_pro/xjzx/info
/home/python/Desktop/flask_news_pro/xjzx
/home/python/Desktop/flask_news_pro/xjzx/logs/log

  

python os.path.dirname() abspath()的更多相关文章

  1. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  2. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  3. Python——os.path.dirname(__file__) 与 os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  4. python os.path.dirname 是什么目录

    这个获取文件路径中所在的目录. 1 2 3 4 5 6 7 In [1]: import os   In [2]: os.__file__ Out[2]: '/usr/lib/python2.7/os ...

  5. python os.path.dirname()

    ----返回文件所在的路径 ----如果path变量直接是文件名则返回空

  6. python - os.path,路径相关操作

    python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...

  7. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  8. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

  9. 第十八篇 模块与包--time&random模块&模块导入import(os.path.dirname(os.path.abspath(__file__)))

    模块 在Python中, 一个.py文件就称为一个模块. 使用模块的好处: 1. 最大的好处就是大大提高了代码的可维护性 2. 编写代码不必从零开始.一个模块编写完毕,就可以被其他地方引用.在写其他程 ...

随机推荐

  1. Java 统计整数二进制中1的个数

    package cookie; public class CountBinary_1 { public static void main(String[] args) { System.out.pri ...

  2. Elasticsearch 过滤

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  3. 课堂测试_WEB界面链接数据库

    课堂测试_WEB界面链接数据库 一,题目: 一. 考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求 ...

  4. 史上最好用的idea激活方法

    最近idea老出现激活一段时间然后就让重新激活的情况,每次都网上搜索一大堆激活方法,各种网址被封,各种插件不能用.就通过朋友介绍搞到一种方式,目前对于2018版本和2019版本都能激活并且正常使用.不 ...

  5. Scrapy的学习和使用

    1.安装 在windows下安装是真心复杂,不搞了,直接在linux下研究吧! sudo pip install scrapy 2.安装遇到问题 2.1.bz2 module not availabl ...

  6. C语言:大数求和

    点击获取题目 1410: [蓝桥杯]高精度加法 时间限制: 1 Sec  内存限制: 256 MB提交: 28  解决: 20[状态] [提交] [命题人:外部导入] 题目描述 输入两个整数a和b,输 ...

  7. 51Nod1085 0-1背包(一维和二维数组实现)

    背包是典型的动态规划问题,关于背包问题的详解,推荐博客:点击打开链接(这篇博客有点错误,代码for循环里错了,不过讲解 的很详细) 题目如下: 在N件物品取出若干件放在容量为W的背包里,每件物品的体积 ...

  8. 类似今日头条,头部tab可滑动,下面的内容可跟着滚动,掺杂着vue和require等用法例子

    1.在main.js里 /*主模块的入口 结合require一起使用*/ require.config({//require的基础用法 配置一下 paths: { "Zepto" ...

  9. [WesternCTF2018]shrine

    0x00 知识点 SSTI模板注入: 模板注入涉及的是服务端Web应用使用模板引擎渲染用户请求的过程 服务端把用户输入的内容渲染成模板就可能造成SSTI(Server-Side Template In ...

  10. STM32速度---网页讲解

    1. ① USART串口,若最大波特率只需115.2k,那用2M的速度就够了,既省电也噪声小. ② I2C接口,若使用400k波特率,若想把余量留大些,可以选用10M的GPIO引脚速度. ③ SPI接 ...