...
Code Block | ||
---|---|---|
| ||
./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.
...
language | bash |
---|
...
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 | ||
---|---|---|
| ||
import os prev_current_directory = os.getcwd() try: os.chdir("/tmp") # Do stuff here finally: os.chdir(prev_current_directory) |
...