I'm writing a test script that is supposed to cd from the current directory into a new one if that path is confirmed to exist and be a directory
serial_number = input("Enter serial number: ")
directory = "/etc/bin/foo"
if os.path.exists(directory) and os.path.isdir(directory):
#cd into directory?
subprocess.call(['cd ..' + directory])
My dilemma is that I don't know how to properly pass a variable into a subprocess command, or whether or not I should use call or Popen. When I try the above code, it comes back with an error saying that No such file or directory "cd ../etc/bin/". I need is to travel back one directory from the current directory so then I can enter /etc and read some files in there. Any advice?
subprocessto runcdis almost always going to be useless; it only changes the working directory for the forked subprocess, leaving the current working directly unchanged oncesubprocess.callreturns. This is also true if you are expecting the working directory to have changed after your Python process exits.