By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,952 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,952 IT Pros & Developers. It's quick & easy.

Get Parent path value (bad design) Help

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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.