splitunc function help

This is the place for queries that don't fit in any of the other categories.

splitunc function help

Postby MadMax3D » Thu Oct 17, 2013 2:01 pm

hey folks,

Brand new member and brand new to Python here. I've been trying to learn python by deconstructing python code and for the most part I'm doing alright. I came across a problem which I'm hoping has an easy solution....at least for those of you in the know.

I'm sourcing files right now that I'd like to be able to toggle between mapped drive letter and unc path through the help of python code. I'm using the os.path.splitdrive() and os.path.splitunc() functions to help me achieve this. The splitdrive function was straightforward. I'm having a bit of trouble with splitunc and I can't seem to find out how to do the following:

Code: Select all
myPath = os.path.splitunc(r'//Network/Path1/Path2/MappedFolder/Path3/Path4')
print myPath
('//Network/Path1', '/Path2/MappedFolder/Path3/Path4')

what I need the output to be is:
Code: Select all
('//Network/Path1/Path2/MappedFolder', '/Path3/Path4')


is there a way within the splitunc function to tell python WHERE the split happens?

thanks.

Dave
Last edited by Mekire on Thu Oct 17, 2013 2:13 pm, edited 1 time in total.
Reason: First post lock. Welcome. Please read the "New Users" post.
MadMax3D
 
Posts: 3
Joined: Thu Oct 17, 2013 1:44 pm

Re: splitunc function help

Postby Kebap » Thu Oct 17, 2013 3:04 pm

Hi, welcome, I have never used this function, yet, but I think it should behave exactly as expected:

http://docs.python.org/2/library/os.path.html wrote:
os.path.splitunc(path)

Split the pathname path into a pair (unc, rest) so that unc is the UNC mount point (such as r'\\host\mount'), if present, and rest the rest of the path (such as r'\path\file.ext'). For paths containing drive letters, unc will always be the empty string.

Availability: Windows.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 366
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: splitunc function help

Postby MadMax3D » Thu Oct 17, 2013 8:13 pm

maybe I'm just not understanding the syntax?

I've read that part of the python help and I understood that the os.path.splitunc() will return a value pair of (unc,rest). What I don't understand is how to define WHERE the mount is?

the following script:

currentFilePath = '//NETWORK/Path1/Path2/MappedFolder/Path3/Path4'
myFileName = os.path.splitunc(currentFilePath)
print myFileName

returns:

('//NETWORK/Path1', '/Path2/MappedFolder/Path3/Path4')

If my drive is mapped: K: ---> \\NETWORK\Path1\Path2\MappedFolder..... how do I get this entire UNC path returned in the above example and NOT be part of the 'rest'?

I need this result returned:

('//NETWORK/Path1/Path2/MappedFolder', '/Path3/Path4')

Is there another way, instead of using the splitunc function, that would give me the same result?
MadMax3D
 
Posts: 3
Joined: Thu Oct 17, 2013 1:44 pm

Re: splitunc function help

Postby snippsat » Thu Oct 17, 2013 10:08 pm

Is there another way, instead of using the splitunc function, that would give me the same result?

You can make your own split function.
Something like this.
Code: Select all
def my_split(s, sp='MappedFolder'):
    pat = s.partition(sp)
    return pat[0]+pat[1], pat[-1]

Test.
Code: Select all
>>> s = r'//Network/Path1/Path2/MappedFolder/Path3/Path4'
>>> my_split(s)
('//Network/Path1/Path2/MappedFolder', '/Path3/Path4')

>>> s = r'//Network/Path1/Path2/new_name/Path3/Path4'
>>> my_split(s, 'new_name')
('//Network/Path1/Path2/new_name', '/Path3/Path4')

User avatar
snippsat
 
Posts: 132
Joined: Thu Feb 21, 2013 12:04 am

Re: splitunc function help

Postby MadMax3D » Fri Oct 18, 2013 5:02 pm

thanks snippsat! That did exactly what I wanted with a slight modification!

Code: Select all
def my_split(s, sp='/MappedFolder'):
    pat = s.partition(sp)
    return pat[-1]
   
s = r'//Network/Path1/Path2/MappedFolder/Path3/Path4'
print "P:" + my_split(s)


The trick is understanding it rather than just copy and pasting it. lol.

Just out of curiosity, when listing the values within pat[], why did you originally use
Code: Select all
return pat[0]+pat[1], pat[-1]
instead of
Code: Select all
return pat[0]+pat[1], pat[2]
What is the purpose of using -1?

While trying to understand your code, I came across another way of achieving the same result. Thought I'd share:

Code: Select all
currentFilePath = '//NETWORK/Path1/Path2/MappedFolder/Path3/Path4'
UNCpath = '//NETWORK/Path1/Path2/MappedFolder'
drivePath = 'P:'

if currentFilePath.startswith(UNCpath):
    newPath = currentFilePath.split(UNCpath)[1]
    print drivePath + newPath


Thanks again.

Dave
MadMax3D
 
Posts: 3
Joined: Thu Oct 17, 2013 1:44 pm

Re: splitunc function help

Postby snippsat » Fri Oct 18, 2013 6:14 pm

What is the purpose of using -1?

[-1] in always the last element in a list.
There are many different combo,you should read more about "slice" for both list and string.
Code: Select all
>>> lst = [1,2,3,4]
>>> lst[-1]
4
>>> lst[3]
4
>>> lst[-2]
3
>>> lst[-2:]
[3, 4]
>>> lst[:-2]
[1, 2]
>>> lst[::2]
[1, 3]
>>> lst[::-2]
[4, 2]
>>> lst[::-1]
[4, 3, 2, 1]
User avatar
snippsat
 
Posts: 132
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: kiranv5 and 2 guests