467,920 Members | 1,101 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

Get Parent path value (bad design) Help

Hi all,

What I am trying to do is write some generic code where I can get the path of the parent directory. for example if current working directory is 'Z:\\dirA\\Parent\\Child'

I want to get a string which up like 'Z:\\dirA\\Parent'
Then I do a sys.path.append('Z:\\dirA\\Parent')

The code I have written below does all this.. but I have a feeling there is a better way to do this.. Please can someone help me.

import os
import sys
pathname = os.getcwd()
pathString = ''
pathList = pathname.split("\\")

pathList.pop() # removes the child
lastItem = pathList.pop() #remove to the parent, will be added back later.
counter = 0
while counter != len(pathList): #build up the path with items in the list
pathString = pathString + pathList[counter] + "\\\\"
counter +=1
pathString = pathString + lastItem #add parent back to the built up path
sys.path.append(pathString) #append the path back to sys.path
Thanks,
Moiz Golawala

Jul 18 '05 #1
  • viewed: 3064
Share:
3 Replies
Golawala, Moiz M (GE Infrastructure) schreef:
Hi all,

What I am trying to do is write some generic code where I can
get the path of the parent directory. for example if current
working directory is 'Z:\\dirA\\Parent\\Child'

I want to get a string which up like 'Z:\\dirA\\Parent'
Then I do a sys.path.append('Z:\\dirA\\Parent')


path = r'Z:\dirA\Parent\Child'

# or: path = os.getcwd()

parent = os.path.normpath(os.path.join(path, '..'))


--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html

Jul 18 '05 #2

"Golawala, Moiz M (GE Infrastructure)" <Mo***********@ge.com> wrote in
message news:ma**************************************@pyth on.org...
Hi all,

What I am trying to do is write some generic code where I can get the path
of the parent directory. for example if current working directory is
'Z:\\dirA\\Parent\\Child'

I want to get a string which up like 'Z:\\dirA\\Parent'
Then I do a sys.path.append('Z:\\dirA\\Parent')

The code I have written below does all this.. but I have a feeling there is
a better way to do this.. Please can someone help me.

import os
import sys
pathname = os.getcwd()
pathString = ''
pathList = pathname.split("\\")

pathList.pop() # removes the child
lastItem = pathList.pop() #remove to the parent, will be added back later.
counter = 0
while counter != len(pathList): #build up the path with items in the list
pathString = pathString + pathList[counter] + "\\\\"
counter +=1
pathString = pathString + lastItem #add parent back to the built up path
sys.path.append(pathString) #append the path back to sys.path
Thanks,
Moiz Golawala
Jul 18 '05 #3
I think what you want is:

import os
pathname=os.getcwd()
parentpath=os.sep.join(pathname.split(os.sep)[:-1])
sys.path.append(parentpath) #append the path back to sys.path

This is shorter and portable.

HTH,
Larry Bates
Syscon, INc.

"Golawala, Moiz M (GE Infrastructure)" <Mo***********@ge.com> wrote in
message news:ma**************************************@pyth on.org...
Hi all,

What I am trying to do is write some generic code where I can get the path
of the parent directory. for example if current working directory is
'Z:\\dirA\\Parent\\Child'

I want to get a string which up like 'Z:\\dirA\\Parent'
Then I do a sys.path.append('Z:\\dirA\\Parent')

The code I have written below does all this.. but I have a feeling there is
a better way to do this.. Please can someone help me.

import os
import sys
pathname = os.getcwd()
pathString = ''
pathList = pathname.split("\\")

pathList.pop() # removes the child
lastItem = pathList.pop() #remove to the parent, will be added back later.
counter = 0
while counter != len(pathList): #build up the path with items in the list
pathString = pathString + pathList[counter] + "\\\\"
counter +=1
pathString = pathString + lastItem #add parent back to the built up path
sys.path.append(pathString) #append the path back to sys.path
Thanks,
Moiz Golawala
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ian Bicking | last post: by
6 posts views Thread by William Gill | last post: by
11 posts views Thread by manstey | last post: by
1 post views Thread by Richard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.