【发布时间】:2023-04-02 15:28:01
【问题描述】:
我正在使用 os.system() 执行 Windows 命令行 shell 执行。我想更改 Windows cmd 当前目录。这是一种方法:
os.chdir('newPath')
但是chdir() 也会改变实际的 Python 当前工作目录。我不想更改实际的 Python 工作目录,因为我希望脚本的其他部分在原始当前工作目录中运行。我要更改的只是 Windows cmd 当前工作目录。换句话说:我希望os.system() 命令在一个当前工作目录(Windows cmd 当前工作目录)中运行,而其他任何东西都应该在另一个当前工作目录(实际的 Python 当前工作目录)中运行。
这是另一个尝试仅更改 Windows cmd 当前目录:
os.system('cd newPath')
但是,这显然不起作用,因为在执行 cd newPath 命令后,Windows cmd 当前目录被重置(因为我不会在下次调用 os.system() 时使用相同的 Windows 命令外壳) .
是否可以为 Windows cmd shell 提供一个单独的当前工作目录? (与实际的当前工作目录分开)。
【问题讨论】:
-
I would like to keep the current working directory while only change the Windows cmd current working directory- 什么? -
@BlueRaja:你是对的。我不清楚。刚刚编辑了问题以使其更清楚。
标签: python cmd working-directory