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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章