os.path.join(path, *paths)

os.path.join(path, *paths)

os.path - Common pathname manipulations (常見路徑操作)
https://docs.python.org/3.7/library/os.path.html

1. os.path.join(path, *paths)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
合理地拼接一個或多個路徑部分。返回值是 path*paths 所有值的連接,每個非空部分後面都緊跟一個目錄分隔符 (os.sep),除了最後一個。這意味着如果最後一部分爲空,則結果將以分隔符 / 結尾。如果參數中某個部分是絕對路徑,則絕對路徑前的路徑都將被丟棄,並從絕對路徑部分開始連接。

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
在 Windows 上,遇到絕對路徑部分 (e.g., r'\foo') 時,不會重置盤符。如果某部分路徑包含盤符,則會丟棄所有先前的部分,並重置盤符。請注意,由於每個驅動器都有一個當前目錄,所以 os.path.join("c:", "foo") 表示驅動器 C: 上當前目錄的相對路徑 (c:foo),而不是 c:\foo

2. example 1

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng

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

import os

path1 = "yong"
path2 = "qiang"
path3 = "cheng"

sum_path = path1 + path2 + path3
merge_path = os.path.join(path1, path2, path3)

print("sum_path = ", sum_path)
print("merge_path = ", merge_path)
/home/yongqiang/miniconda3/envs/tf_cpu_1.4.1/bin/python /home/yongqiang/pycharm_work/yongqiang.py
sum_path =  yongqiangcheng
merge_path =  yong/qiang/cheng

Process finished with exit code 0

3. example 2

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng

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

import os

path1 = "yong"
path2 = "/qiang"
path3 = "cheng"

sum_path = path1 + path2 + path3
merge_path = os.path.join(path1, path2, path3)

print("sum_path = ", sum_path)
print("merge_path = ", merge_path)
/home/yongqiang/miniconda3/envs/tf_cpu_1.4.1/bin/python /home/yongqiang/pycharm_work/yongqiang.py
sum_path =  yong/qiangcheng
merge_path =  /qiang/cheng

Process finished with exit code 0

4. example 3

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng

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

import os

path1 = "/yong/"
path2 = "/qiang/"
path3 = "cheng/"

sum_path = path1 + path2 + path3
merge_path = os.path.join(path1, path2, path3)

print("sum_path = ", sum_path)
print("merge_path = ", merge_path)
/home/yongqiang/miniconda3/envs/tf_cpu_1.4.1/bin/python /home/yongqiang/pycharm_work/yongqiang.py
sum_path =  /yong//qiang/cheng/
merge_path =  /qiang/cheng/

Process finished with exit code 0

5. example 4

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng

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

import os

path1 = "/yong"
path2 = "/qiang"
path3 = "/cheng"

sum_path = path1 + path2 + path3
merge_path = os.path.join(path1, path2, path3)

print("sum_path = ", sum_path)
print("merge_path = ", merge_path)
/home/yongqiang/miniconda3/envs/tf_cpu_1.4.1/bin/python /home/yongqiang/pycharm_work/yongqiang.py
sum_path =  /yong/qiang/cheng
merge_path =  /cheng

Process finished with exit code 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章