2025-09-10:仓库迁移
This commit is contained in:
20
5.文件与IO/11.处理路径.py
Normal file
20
5.文件与IO/11.处理路径.py
Normal file
@@ -0,0 +1,20 @@
|
||||
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))
|
Reference in New Issue
Block a user