How do I change directory back to my original working directory with Python?

前端 未结 6 804
梦谈多话
梦谈多话 2020-12-15 04:07

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

6条回答
  •  时光说笑
    2020-12-15 04:41

    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.

提交回复
热议问题