python os.path.dirname() abspath()
测试文件的名称
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()的更多相关文章
- 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__)返回 ...
- 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 ...
- 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 ...
- 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 ...
- python os.path.dirname()
----返回文件所在的路径 ----如果path变量直接是文件名则返回空
- python - os.path,路径相关操作
python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...
- os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用
python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...
- 第十八篇 模块与包--time&random模块&模块导入import(os.path.dirname(os.path.abspath(__file__)))
模块 在Python中, 一个.py文件就称为一个模块. 使用模块的好处: 1. 最大的好处就是大大提高了代码的可维护性 2. 编写代码不必从零开始.一个模块编写完毕,就可以被其他地方引用.在写其他程 ...
随机推荐
- 安装完CUDA Toolkit,VS2010调试项目控制台一闪而过
选择菜单栏的调试>>开始执行(不调试),就不一闪而过:
- hdu 1277 全文检索 (字典树应用)
全文检索 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- element-ui实现自定义多个文件上传
这里强调下是:aixos是原始的,不要qs封装过的,不然不识别传值传不过去 <el-upload action="/admin/borrow/borrowEdit" list ...
- es6的...
es6的... (1):函数rest参数和扩展 sum1(...sum){//传多个参数 let total=0; for( ...
- 目标检测算法的总结(R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD、FNP、ALEXnet、RetianNet、VGG Net-16)
目标检测解决的是计算机视觉任务的基本问题:即What objects are where?图像中有什么目标,在哪里?这意味着,我们不仅要用算法判断图片中是不是要检测的目标, 还要在图片中标记出它的位置 ...
- idea将web项目打成war包放在tomcat/webapps上运行
1.进入Project Structure 或者 file -> Project Structure 或者 快捷键ctrl+alt+shift+s 2.选中Artifacts 3.点加号,然后如 ...
- 实验吧Web-难-头有点大(http头伪造:浏览器、国家、.Net framework版本)
进去网站显示: 此处告诉我们要干三件事: (1).net framework 版本为9.9 (2)告诉服务器我们的地址为英国 (3)我们访问站点用的是IE 下面我们就抓的包中伪造. 1:.net fr ...
- Day 4:集合——迭代器与List接口
Collection-迭代方法 1.toArray() 返回Object类型数据,接收也需要Object对象! Object[] toArray(); Collection c = new Arra ...
- SAP_ABAP常用事务代码
ST05: 性能跟踪(Performance Trace) SE30/SAT: ABAP对象性能分析 ST12: 单个对象性能分析(Single transaction analysis)
- Java基础之反射、注解、代理
反射 笔者对反射的理解就是解剖class文件,来进行一系列操作. Class类 获取Class类实例的三种方式: 类名.class 对象.getClass() static Class forName ...