python - os.getcwd() 返回一个斜杠 (/)
问题描述
我有一个在 macOS 上的 Atom 编辑器中编写的 python 3.6 脚本。该脚本经常使用 os.getcwd() 并且一直运行良好。我昨晚重新启动了我的电脑,更新了 Atom 包,突然它坏了。使用打印语句, os.getcwd() 现在只返回“/”。
尝试了多个脚本,都在不同的目录中。
import os
print(os.getcwd())
预期的:/Users/kpaddock/Desktop/Python/SCID-Report-DRAFT
实际输出:/
解决方案
我的错,我看的不够仔细。原来os.getcwd()
并不总是准确的?替换为os.path.abspath(os.path.dirname(__file__))
,它工作得很好。
推荐阅读
- jquery - box-shadow 仅显示在图像的左侧
- project-server - 使用 Rest API 进入 Project Server 的时间表条目
- reactjs - 将表单数据保存为用户类型(React/Postgres)
- android - 应用关闭时的AlarmManager完成服务(LocationManager)
- ms-access - 从另一个表单打开表单到特定记录 Access VBA 2016
- spring - spring boot unsatisfied dependency 通过字段 customerDetailsSerice 嵌套异常表示
- azure - 如何使用 ARM 模板创建包含多个私有 IP 地址的 ipConfiguration 的 Azure VM
- macos - 在 macOS 上让 OpenGL 与 Core Animation 共存
- ios - Opencv iOS 类型
- django - 如何在不区分大小写的情况下执行 model.objects.get(**kwargs)