我是靠谱客的博主 着急冰淇淋,最近开发中收集的这篇文章主要介绍os.curdir - os.getcwd() - os.path.dirname(os.path.abspath(__file__)) 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) 返回的结果一样。

最后

以上就是着急冰淇淋为你收集整理的os.curdir - os.getcwd() - os.path.dirname(os.path.abspath(__file__)) os.curdir - os.getcwd() - os.path.dirname(os.path.abspath( file)) 的全部内容,希望文章能够帮你解决os.curdir - os.getcwd() - os.path.dirname(os.path.abspath(__file__)) os.curdir - os.getcwd() - os.path.dirname(os.path.abspath( file)) 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部