1

I have imported a existing filesystem folder as the new project folder in eclipse. I have a script which get the current working directory path of the code. I need to change directory location to acccess files in other directory related to it. But It is giving different value when executed from eclipse and from the command line. Location is same in both place. Please help me resolve this issue. Operating system is windows here

import os
print os.getcwd()
os.chdir(os.path.dirname(os.getcwd()))
print os.getcwd()

One result is this

C:\Automation\trunk\Base\TestScripts
C:\Automation\trunk\Base

Other result is this

C:\Automation\trunk\UsefulScripts
C:\Automation\trunk

Second result is the one I expect, and that is where the code is located exactly.

  • Please provide the different results that you get. Please specify with which OS you work, as all functions are not available on all OSs. – Lescurel Sep 08 '17 at 13:34
  • When your code runs in eclipse you have a certain amount of environment variables including ECLIPSE_HOME, PARENT_LOC, PROJECT_LOC and WORKSPACE_LOC. Use them to build your Path. You cannot rely on getcwd() when running in eclipse. On the command line you can. Just check for existense of these variables. If present use them if not ... use getcwd() – mbieren Sep 08 '17 at 13:36
  • @Lescurel, i have provided those info. – Saranya Sridharan Sep 08 '17 at 13:38
  • @mbieren, I believe the code should be generic to run in both. how should attain that. – Saranya Sridharan Sep 08 '17 at 13:38
  • of course. I mean you have to code this in your function like `loc = os.environ.get('PROJECT_LOC',None)`. If loc is none use getcwd otherwise use loc as basedir for your UsefulScripts – mbieren Sep 08 '17 at 13:39
  • i wonder, how would i know through which way , user will be running the code. how could i identify whether its eclipse or command line ? @mbieren – Saranya Sridharan Sep 08 '17 at 13:41
  • ok i will try that too @mbieren. Thank you so much – Saranya Sridharan Sep 08 '17 at 13:47
  • Thanks a lot @Lescurel – Saranya Sridharan Sep 08 '17 at 13:47

2 Answers2

0

watch out you cannot rely on that. Do the following :

basedir = os.environ.get('PROJECT_LOC', None)

if not basedir:
     basedir = sys.path[0]    # We are on commandline. sys.path OK

Then use basedir to find your files

Update

you have to specify this variable in the runtime of the interpreter

window->preferences->PyDev->Interpreters->Python Interpreter TAB (environment) There you can specify PROJECT_LOC referring to project_loc by selecting NEW with name PROJECT_LOC and variable (the other button) and selecting project_loc.

For some reasons those variables are not visible in python.

You can check this now with

def read_all_variables():
    for key in os.environ.keys():
        print ("%30s %s" % (key,os.environ[key]))

PROJECT_LOC should be there now

mbieren
  • 813
  • 4
  • 24
-1

I have used the sys package instead of os. It works as expected.

import os,sys
currentpath = sys.path[0]
print currentpath

I can able to run it from eclipse and also from command line to get the correct path. Thanks for the help .