首页 > 解决方案 > os.getcwd() 返回一个斜杠 (/)

问题描述

我有一个在 macOS 上的 Atom 编辑器中编写的 python 3.6 脚本。该脚本经常使用 os.getcwd() 并且一直运行良好。我昨晚重新启动了我的电脑,更新了 Atom 包,突然它坏了。使用打印语句, os.getcwd() 现在只返回“/”。

尝试了多个脚本,都在不同的目录中。

import os
print(os.getcwd())

预期的:/Users/kpaddock/Desktop/Python/SCID-Report-DRAFT

实际输出:/

标签: pythonmacospathatom-editorgetcwd

解决方案


我的错,我看的不够仔细。原来os.getcwd()并不总是准确的?替换为os.path.abspath(os.path.dirname(__file__)),它工作得很好。


推荐阅读