如果您使用 macOS 那么您就会知道如何在终端 中输入 cd ~/Documents
并且无论计算机的用户是谁,CWD 都会更改为文档。
在 python 中,我尝试使用 os.chdir("~/Documents")
使用 os 模块 做同样的事情> 但它不起作用!
有人知道另一种方法吗?
最佳答案
How do I use '~' (tilde) in the context of paths?
根据上述描述,您的问题的一个解决方案是 os.chdir(os.path.expanduser('~/Documents'))
关于python - python os 模块中的 os.chdir(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244634/