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. 编写代码不必从零开始.一个模块编写完毕,就可以被其他地方引用.在写其他程 ...
随机推荐
- docker 安装好后启动异常解决
一个月前在虚拟机中根据视频教程安装了docker 启动docker后执行 systemctl status docker 出现了异常,具体如下: [root@joinApp2 ~]# systemct ...
- (HN)AHOI2018 转盘
题意: 有 \(n\) 个格子围成一圈,每个格子里有一个物品,每个物品的出现时间为 \(T_i\) .开始时选择一个格子为起点,每个单位时间可以向前走一格或不动,若当前格的物品已出现则将其标记.有 \ ...
- PHP笔记01
php 环境 xamp wamp phpstudy等集成软件网上很多 PHP基础语法 PHP语法是以<?php开始 ?>结束的//php 文件的默认扩展名是.php 例如(用PHP输出he ...
- hdu 1257 最少拦截系统 求连续递减子序列个数 (理解二分)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- P 1033 旧键盘打字
转跳点:
- UVA - 714 Copying Books (抄书)(二分+贪心)
题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列).设第i个序列的 ...
- ssh and scp从远程服务器下载文件
scp -r root@172.16.252.32:/home/files /home/files 下载目录 -r root是用户172.16.252.32是ip:/home/files 是你要 ...
- JS事件-对象监视事件,事件触发函数
写在前面: 一句话作为本文的核心思想:对象监视事件,事件触发函数: (一)事件 1.事件分类 (1)鼠标事件 click() 鼠标单击 dblclick() ...
- 抓DHCP客户端ip脚本
cat testnew.sh #!/bin/bash catch_ip (){Ip=`sudo nmap -sP 192.168.1.0/24 |grep -i -B2 $mac|grep Nmap ...
- CMakeLists添加内部库
SET(RTABMap_LIBRARIES ${PROJECT_SOURCE_DIR}/bin/librtabmap_core.so ${PROJECT_SOURCE_DIR}/bin/librtab ...