测试文件的名称

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. 大数据之虚拟机配置和环境准备及hadoop集群搭建

    一.VMnet1和VMnet8路由器 VMware-workstation软件选择默认安装时,会自动创建VMnet1和VMnet8路由器设备.(安装失败使用CCleaner清理vm软件) VMnet1 ...

  2. UVA - 1213 Sum of Different Primes (不同素数之和)(dp)

    题意:选择k个质数,使它们的和等于n,问有多少种方案. 分析:dp[i][j],选择j个质数,使它们的和等于i的方法数. #pragma comment(linker, "/STACK:10 ...

  3. microsoft help viewer 收藏夹功能

    平时重装系统比较多,重装后,microsoft help viewer 2.0里面的收藏就丢失了,要恢复以前的收藏,可以直接在C:\Users\ZR\AppData\Local\Microsoft\H ...

  4. 2. Jetson TX2--python3下编译安装opencv3.4

    https://cloud.tencent.com/developer/article/1327273 jetpack3.2自带了opencv3.3,但是只提供了python2.7的编译版本,所以也只 ...

  5. C++ STD Gems06

    generate.generate_n.sample.iota #include <iostream> #include <vector> #include <strin ...

  6. 轻量级UILabel分段点击扩展更新啦

    http://www.code4app.com/thread-31445-1-1.html Tag: 项目介绍: YBAttributeTextTapAction 一行代码添加文本点击事件 效果图 S ...

  7. 翻翻棋(找规律问题)(FZU Problem 2230)

    题目是这样的: FZU Problem 2230 象棋翻翻棋(暗棋)中双方在4*8的格子中交战,有时候最后会只剩下帅和将.根据暗棋的规则,棋子只能上下左右移动,且相同的级别下,主动移动到地方棋子方将吃 ...

  8. [SUCTF 2019]EasyWeb

    0x00 知识点 本题知识量巨大,把我给看傻了..盯着网上师傅们的博客看了好久.. 知识点1 构造不包含数字和字母的webshell 思路来自p牛 参考链接: https://www.leaveson ...

  9. mybatis+mysql 通过sql脚本生成mapper的部分内容

    SQL mysql SELECT concat('<if test="', COLUMN_NAME, ' != null"> ', COLUMN_NAME, ',< ...

  10. VUE获取焦点

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...