os.chdir allows you to change the working directory as you wish (you can extract the working directory cfg_path using os.path.dirname ); remember to get the current os.getcwd directory first if you want to restore it when you finish exec'ing cfg_path .
Python 3 really removes the execfile (in favor of the sequence in which you read the file, compile contents, and then exec them), but you donβt have to worry about this if you are currently coding in Python 2.6, since the source 2to3 source process all this is smooth and fluid.
Edit : The OP reports in a comment that execfile starts a separate process and does not respect the current working directory. This is not true, and here is an example showing that it is:
import os def makeascript(where): f = open(where, 'w') f.write('import os\nprint "Dir in file:", os.getcwd()\n') f.close() def main(): where = '/tmp/bah.py' makeascript(where) execfile(where) os.chdir('/tmp') execfile(where) if __name__ == '__main__': main()
Running this on my computer produces output, for example:
Dir in file: /Users/aleax/stko Dir in file: /private/tmp
clearly shows that execfile does continue to use the working directory installed at the time execfile executed. (If the executable changes the working directory that will be displayed after the execfile - precisely because everything happens in the same process!).
Thus, any problems that the OP is still observing are not tied to the current working directory (it is difficult to diagnose what they may actually be without seeing the code and accurate information about the problems observed;).
Alex martelli
source share