os.curdir - os.getcwd() - os.path.dirname(os.path.abspath(__file__))

os.curdir - os.getcwd() - os.path.dirname(os.path.abspath( file))

English - os - Miscellaneous operating system interfaces
https://docs.python.org/3/library/os.html

Simplified Chinese - os - 各种各样的操作系统接口
https://docs.python.org/zh-cn/3/library/os.html

miscellaneous [ˌmɪsəˈleɪniəs]:adj. 混杂的,各种各样的,多方面的,多才多艺的

1. os.curdir

The constant string used by the operating system to refer to the current directory. This is . for Windows and POSIX. Also available via os.path.

2. os.getcwd()

Return a string representing the current working directory.

3. yongqiang.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys

if __name__ == '__main__':
    current_directory = os.path.dirname(os.path.abspath(__file__))
    print("current_directory:", current_directory)
    print("__file__:", __file__)
    print("os.path.abspath(__file__):", os.path.abspath(__file__))
    print("os.path.dirname(os.path.abspath(__file__)):", os.path.dirname(os.path.abspath(__file__)))
    print("os.getcwd():", os.getcwd())
    print("os.curdir:", os.curdir)
    print("os.path.abspath(os.curdir):", os.path.abspath(os.curdir))
    print("sys.path[0]:", sys.path[0])

/home/strong/sunergy_moonergy_work/object_detection_example

strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ pwd
/home/strong/sunergy_moonergy_work/object_detection_example
strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ python3 yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work/object_detection_example
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work/object_detection_example
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$


strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ python ./yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: ./yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work/object_detection_example
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work/object_detection_example
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$


strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ python3 /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work/object_detection_example
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work/object_detection_example
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ 

/home/strong/sunergy_moonergy_work

strong@foreverstrong:~/sunergy_moonergy_work/object_detection_example$ cd ../
strong@foreverstrong:~/sunergy_moonergy_work$ pwd
/home/strong/sunergy_moonergy_work
strong@foreverstrong:~/sunergy_moonergy_work$ python3 object_detection_example/yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~/sunergy_moonergy_work$ 


strong@foreverstrong:~/sunergy_moonergy_work$ python3 ./object_detection_example/yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: ./object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~/sunergy_moonergy_work$ 


strong@foreverstrong:~/sunergy_moonergy_work$ python3 /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong/sunergy_moonergy_work
os.curdir: .
os.path.abspath(os.curdir): /home/strong/sunergy_moonergy_work
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~/sunergy_moonergy_work$ 

/home/strong

strong@foreverstrong:~/sunergy_moonergy_work$ cd ..
strong@foreverstrong:~$ pwd
/home/strong
strong@foreverstrong:~$ python3 sunergy_moonergy_work/object_detection_example/yongqiang.py
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong
os.curdir: .
os.path.abspath(os.curdir): /home/strong
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~$ 


strong@foreverstrong:~$ python3 ./sunergy_moonergy_work/object_detection_example/yongqiang.py
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: ./sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong
os.curdir: .
os.path.abspath(os.curdir): /home/strong
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~$ 


strong@foreverstrong:~$ python3 /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py 
current_directory: /home/strong/sunergy_moonergy_work/object_detection_example
__file__: /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.abspath(__file__): /home/strong/sunergy_moonergy_work/object_detection_example/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/sunergy_moonergy_work/object_detection_example
os.getcwd(): /home/strong
os.curdir: .
os.path.abspath(os.curdir): /home/strong
sys.path[0]: /home/strong/sunergy_moonergy_work/object_detection_example
Hello, Yongqiang!
strong@foreverstrong:~$

os.getcwd()os.curdir 用于获取当前执行 Python 文件的文件夹,直接使用 os.curdir 时会返回 . (表示当前路径),返回的是当前执行 Python 文件的文件夹,而不是 python 文件所在的文件夹。os.getcwd()os.path.abspath(os.curdir) 返回的结果一样。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值