Python: execfile from other file's working directory?

Os. Chdir lets you change the working directory as you wish (you can extract the working directory of cfg_path with os.path. Dirname ); be sure to first get the current directory with os.

Getcwd if you want to restore it when you're done exec'ing cfg_path.

Os. Chdir lets you change the working directory as you wish (you can extract the working directory of cfg_path with os.path. Dirname); be sure to first get the current directory with os.

Getcwd if you want to restore it when you're done exec'ing cfg_path. Python 3 does indeed remove execfile (in favor of a sequence where you read the file, compile the contents, then exec them), but you need not worry about that, if you're currently coding in Python 2.6, since the 2to3 source to source translation deals with all this smoothly and seamlessly. Edit: the OP says, in a comment, that execfile launches a separate process and does not respect the current working directory.

This is false, and here's 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 machine produces output such as: Dir in file: /Users/aleax/stko Dir in file: /private/tmp clearly showing that execfile does keep using the working directory that's set at the time execfile executes. (If the file executed changes the working directory, that will be reflected after execfile returns -- exactly because everything is taking place in the same process! ).

So, whatever problems the OP is still observing are not tied to the current working directory (it's hard to diagnose what they may actually be, without seeing the code and the exact details of the observed problems;-).

Thanks for your answer. Unfortunately, this doesn't seem to work for me. Execfile() appears to be launching an entirely new process with its own working directory, which isn't the same as the one I'm launching from.

– Kyle Kaitan Nov 28 '09 at 19:09 No, @Kyle, execfile does not launch a separate process, and it does respect the current working directory. Editing my answer to show a simple example proving that you're wrong. – Alex Martelli Nov 28 '09 at 21:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions