I coincidentally found out that I cannot change the actual directory from within a python-code. My Test-Program is as follows:
from os import system
def sh(script):
system("bash -c '%s'" % script)
sh("cd /home")
sh("pwd")
The output of pwd is not /home, but the directory where the code above lives.
Can someone explain why this happens?
The problem is that you execute shell commands instead of actually changing the directory using
os.chdir()Each
os.system()call executes the given command in a new shell – so the script’s working directory is not affected at all.