[英]Splitting Drive from filepath
On Windows platform it is quite simple to separate the drive letter from a filepath using npath module: 在Windows平台上,使用npath模块将驱动器号与文件路径分开非常简单:
import ntpath
filepath='c:\\my_drivepath\\somefolder\\blabla\\filename.txt'
result = ntpath.splitdrive(filepath)
print result
prints out: 打印出:
('c:', '\\my_drivepath\\somefolder\\blabla\\filename.txt') <type 'tuple'>
But using it with Mac filepath: 但是将其与Mac文件路径一起使用:
filepath='/Volumes/drivename/Folder1/Folder2/Folder3/Folder4/Filename.ext'
results to: 结果为:
('', '/Volumes/drivename/Folder1/Folder2/Folder3/Folder4/Filename.ext') <type 'tuple'>
I wonder if there any module/method/command available that splits drive name from Mac file path... looking for the output like this: 我想知道是否有可用的模块/方法/命令将驱动器名称从Mac文件路径中分离出来...寻找这样的输出:
('/Volumes/drivename', '/Folder1/Folder2/Folder3/Folder4/Filename.ext')
Mac is a Unix based OS and its concept of "drive" is different from Windows. Mac是基于Unix的操作系统,其“驱动器”的概念不同于Windows。 In Unix all directories are starting from /
and "drives" can be mounted in any directory under /
. 在Unix中,所有目录都从/
开始,“驱动器”可以安装在/
下的任何目录中。
In your case, it is better to use split
to do your job: 在您的情况下,最好使用split
来完成您的工作:
>>> d = '/Volumes/drivename/Folder1/Folder2/Folder3/Folder4/Filename.ext'
>>> d.split('/',3)
['', 'Volumes', 'drivename', 'Folder1/Folder2/Folder3/Folder4/Filename.ext']`
In Unix based systems, a drive can be mapped to any directory. 在基于Unix的系统中,驱动器可以映射到任何目录。 So it can be at 所以可以在
/Volumes/drivename /卷/ DRIVENAME
or 要么
/Users/dir1/dir2/dir3/dir4' /用户/ DIR1 / DIR2 / DIR3 / dir4'
You may want to use a command line utility such as df
, to find which drives exist, and where they are mapped. 您可能要使用df
类的命令行实用程序来查找存在的驱动器以及它们的映射位置。 Then you would find out what the "drive path" is. 然后,您将找出“驱动器路径”是什么。
$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 236003080 139929200 84062516 63% /
udev devtmpfs 10240 0 10240 0% /dev
tmpfs tmpfs 805524 968 804556 1% /run
tmpfs tmpfs 5120 0 5120 0% /run/lock
tmpfs tmpfs 3642440 21384 3621056 1% /run/shm
none tmpfs 4 0 4 0% /sys/fs/cgroup
Here you can see my main drive is mounted to /
. 在这里,您可以看到我的主驱动器已安装到/
。 If I had a USB drive, you would see something like /media/usb0
如果我有USB驱动器,则会看到类似/media/usb0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.