I have a function that resembles the one below. I\'m not sure how to use the os module to get back to my original working directory at the conclusion of the jar\'s execution
A context-manager is overkill for this situation (executing a system command). The best solution is to use the subprocess
module instead (Python 2.4 onwards) and the run
or popen
methods with the cwd
argument.
So, your code can be replaced with:
def run():
#run jar from test directory
subprocess.run(cmd, cwd=testDir)
See https://bugs.python.org/issue25625 and https://docs.python.org/3/library/subprocess.html#subprocess-replacements.