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")
joaquin
  • 82,968
  • 29
  • 138
  • 152
spring
  • 13
  • 1
  • 4

3 Answers3

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

should work.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Importing everyhing into the global scope is pretty messy. – ThiefMaster Jul 12 '11 at 06:14
  • 1
    @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')` – phihag Jul 12 '11 at 06:18
  • 2
    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... – ThiefMaster Jul 12 '11 at 06:20
  • 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. – spring Jul 12 '11 at 06:32
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")
rotoglup
  • 5,223
  • 25
  • 37
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'
jd.
  • 10,678
  • 3
  • 46
  • 55
  • 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 – spring Jul 12 '11 at 06:37
  • @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. – jd. Jul 12 '11 at 06:44
  • @spring What's the output of `os.path.join(os.getcwd(), '..\\Common')`? – jd. Jul 12 '11 at 06:47
  • 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. – jd. Jul 12 '11 at 07:11