alphanumeric
alphanumeric

Reputation: 19379

Splitting Drive from filepath

On Windows platform it is quite simple to separate the drive letter from a filepath using npath module:

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:

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:

('/Volumes/drivename', '/Folder1/Folder2/Folder3/Folder4/Filename.ext') 

Upvotes: 0

Views: 63

Answers (2)

Martin Konecny
Martin Konecny

Reputation: 59701

In Unix based systems, a drive can be mapped to any directory. So it can be at

/Volumes/drivename

or

/Users/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. 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

Upvotes: 2

Suku
Suku

Reputation: 3880

Mac is a Unix based OS and its concept of "drive" is different from Windows. In Unix all directories are starting from / and "drives" can be mounted in any directory under /.

In your case, it is better to use split to do your job:

>>> d = '/Volumes/drivename/Folder1/Folder2/Folder3/Folder4/Filename.ext'
>>> d.split('/',3)
['', 'Volumes', 'drivename', 'Folder1/Folder2/Folder3/Folder4/Filename.ext']`

Upvotes: 2

Related Questions