如何在Python中将路径拆分

You can get the drive and path+file like this:

drive, path_and_file = os.path.splitdrive(path)

Get the path and the file:

path, file = os.path.split(path_and_file)

第二种方法 os.path.normpath

import os
path = os.path.normpath(path)
path.split(os.sep)

var = “d:\stuff\morestuff\furtherdown\THEFILE.txt”

[ “d”, “stuff”, “morestuff”, “furtherdown”, “THEFILE.txt” ]

发布了236 篇原创文章 · 获赞 157 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/MrCharles/article/details/103169377