1

I am writing a python script 2.5 in Windows whose CurrentDir = C:\users\spring\projects\sw\demo\753\ver1.1\011\rev120\source my file is test.py. From this path I would like to access files in this path: C:\users\spring\projects\sw\demo\753\ver1.1\011\rev120\Common\

I tried using os.path.join but it does not work and I from the docs I understand why. So what could be the best pythonic solution for this?

currentdir = os.getcwd()    
config_file_path =  os.path.join(currentdir,"\\..\\Common")
6
  • Post some code. What's not working? Commented Jul 12, 2011 at 6:05
  • Code snippet would be helpful in tracking down the problem. Commented Jul 12, 2011 at 6:06
  • Why are you using an ancient python version? Commented Jul 12, 2011 at 6:14
  • @ThiefMaster My environment has a number of internal tools all based on python 2.5 so can't break it Commented Jul 12, 2011 at 6:29
  • Sounds like those tools should be tested with 2.6/2.7 (which should be easy since those versions do not introduce many changes which might break things) instead of keeping such an old version. Commented Jul 12, 2011 at 6:30

3 Answers 3

2

Your problem can be solved by using os.path.join, but you're not using it properly.

currentdir = os.getcwd()    
config_file_path =  os.path.join(currentdir,"\\..\\Common")

"\\..\\Common" is not a relative path, as it starts with \.

You need to join with ..\\Common, which is a relative path.

Please note that os.path.join is not a simple string concatenation function, you don't need to insert the in-between antislashes.

So fixed code would be :

config_file_path =  os.path.join(currentdir,"..\\Common")

or, alternatively :

config_file_path =  os.path.join(currentdir, "..", "Common")
Sign up to request clarification or add additional context in comments.

Comments

2
from os.path import dirname, join
join(dirname(dirname(__file__)), 'Common')

should work.

4 Comments

Importing everyhing into the global scope is pretty messy.
@ThiefMaster Just importing two functions hardly counts as "everything", and makes the line look less cryptic. Of course, you can write os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Common')
In a small script you are right, but in a module with a few hundred SLOC someone won't expect join to be from os.path but rather something more generic...
For my specific problem it may work but what about if I want to access files in C:\users\spring\projects\sw\demo\753\ver1.1 ? I will have to use dirname a number of times.
0

Try this:

joined = os.path.join('C:\\users\\spring\\projects\\sw\\demo\\753\\ver1.1\\011\\rev120\\source', '..\\Common\\')
# 'C:\\users\\spring\\projects\\sw\\demo\\753\\ver1.1\\011\\rev120\\source\\..\\Common\\'
canonical = os.path.realpath(joined)
# 'C:\\users\\spring\\projects\\sw\\demo\\753\\ver1.1\\011\\rev120\\Common'

4 Comments

I get my current dir using os.getcwd() which returns 'C:\users\spring\projects\sw\demo\753\ver1.1\011\rev120\source' and when I try to use join I only get C:\common which is not I want
@ThiefMaster The comment is a copy-paste from the Python interpreter. join() did not remove the ... I agree though that the path is usable as it is and the call to realpath() is completely unnecessary.
@spring What's the output of os.path.join(os.getcwd(), '..\\Common')?
I get `C:\users\spring\projects\sw\demo\753\ver1.1\011\rev120\source\..\Common` when I substitute your path for os.getcwd(). I don't know why we are getting different results. I'm running Python 2.7 on Windows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.