如何在python中从给定路径中剪切驱动器名称

时间:2015-04-12 06:53:31

标签: python

在python中我想从给定的路径中剪切驱动器名称,

Example:
if path given is D:\John\myfolder\newfolder
then I should get John\myfolder\newfolder

请告诉我该怎么做?我正在尝试使用os.path.abspath(路径),但这不起作用......

2 个答案:

答案 0 :(得分:4)

使用os.path.splitdrive

In [1]: import os

In [2]: drive, tail = os.path.splitdrive(r'D:\John\myfolder\newfolder')

In [3]: tail
Out[3]: '\\John\\myfolder\\newfolder'

答案 1 :(得分:0)

你可以使用切片。

>>> path = r"D:\John\myfolder\newfolder"
>>> print(path[3:])
John\myfolder\newfolder