Versions Compared

Key

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

...

Code Block
languagebash
./program.py                     # Current directory is in the same directory as(parent the program file
directory)
./directory/program.py           # Current directory is at1 thedirectory parectup directoryfrom of the programparent filedirectory
/home/root/directory/program.py  # Current directory could be anywhere in the filesystem

...

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 programthe code 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)

...