# -*- coding: utf-8 -*- """ @Time: 2018/9/26 @Author: songhao @微信公众号: zeropython @File: test_file_os.py """ import os import requests # 相对路径 print("相对路径","../") print("相对路径的父级",os.path.dirname("../")) print("相对路径的上一级",os.path.abspath(os.path.dirname("../"))) # 绝对路径 print("requests模块路径",requests.__file__) print("__file__",__file__) print("__file__ 上一级",os.path.dirname(__file__)) print("__file__ 绝对路径",os.path.abspath(__file__))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
"""
@Time: 2018/9/26
@Author: songhao
@微信公众号: zeropython
@File: test_file_os.py
"""
import os
 
import requests
 
 
# 相对路径
print ( "相对路径" , "../" )
print ( "相对路径的父级" , os.path . dirname ( "../" ) )
 
print ( "相对路径的上一级" , os.path . abspath ( os.path . dirname ( "../" ) ) )
 
 
 
# 绝对路径
print ( "requests模块路径" , requests . __file__ )
print ( "__file__" , __file__ )
print ( "__file__ 上一级" , os.path . dirname ( __file__ ) )
print ( "__file__ 绝对路径" , os.path . abspath ( __file__ ) )
 
 

输出

/usr/local/anaconda3/bin/python3 /Users/songhao/py/flask_demo/test_file_os.py ../ 相对路径的父级 .. 相对路径的上一级 /Users/songhao/py requests模块路径 /usr/local/anaconda3/lib/python3.6/site-packages/requests/__init__.py __file__ /Users/songhao/py/flask_demo/test_file_os.py __file__ 上一级 /Users/songhao/py/flask_demo __file__ 绝对路径 /Users/songhao/py/flask_demo/test_file_os.py
1
2
3
4
5
6
7
8
9
10
/ usr / local / anaconda3 / bin / python3 / Users / songhao / py / flask_demo / test_file_os . py
. . /
相对路径的父级 . .
相对路径的上一级 / Users / songhao / py
requests模块路径 / usr / local / anaconda3 / lib / python3 . 6 / site - packages / requests / __init__ . py
__file__ / Users / songhao / py / flask_demo / test_file_os . py
__file_ _ 上一级 / Users / songhao / py / flask_demo
__file_ _ 绝对路径 / Users / songhao / py / flask_demo / test_file_os . py
 
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐