Files
Python_CookBook_repo/5.文件与IO/11.处理路径.py
2025-09-10 16:12:45 +08:00

21 lines
675 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
if __name__ == '__main__':
path = '/Users/beazley/Data/data.csv'
# 如果我们想要得到地址的最后一部分可以使用basename字段
print(os.path.basename(path))
# 前面的部分作为文件夹可以使用dirname读出来
print(os.path.dirname(path))
# 如果想要拼接多个路径则可以使用join
print(os.path.join('suka', 'blyet', os.path.basename(path)))
# 想要提取出文件名可以使用splitext
print(os.path.splitext(os.path.basename(path)))
# 也可以使用expanduser展开上级目录,默认接控制台下的路径
path = "~\practice"
print(os.path.expanduser(path))