Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Your program (whether it’s Python or a shell script) should avoid changing the caller’s current directory if possible.

...

languagebash

...

In Python, changing the current directory could potentially affect code further in the code stack.

If the current directory needs to be changed for any reason, ensure it gets reverted by the end of the programcode block that needs the current directory to be at the specific location.

Code Block
languagepy
import os

prev_current_directory = os.getcwd()
try:
    os.chdir("/tmp")
    # Do stuff here
finally:
    os.chdir(prev_current_directory)

...