return context manager which changes to current dir during the managed "with" context. On __enter__ it returns the old dir.
因此,您所观察到的行为是正确的:
def test_something(tmpdir):
print('current directory where you are before changing it:', os.getcwd())
# the current directory will be changed now
with tmpdir.as_cwd() as old_dir:
print('old directory where you were before:', old_dir)
print('current directory where you are now:', os.getcwd())
print('you now returned to the old current dir', os.getcwd())
查看^{} 的文档:
因此,您所观察到的行为是正确的:
请记住,您的示例中的
p
不是您要更改到的“新”当前目录,而是您从中更改的“旧”目录。在From the documentation:
而,
getcwd
代表Get Current Working directory,并返回启动python进程的目录。在相关问题 更多 >
编程相关推荐