V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tin
V2EX  ›  Python

请教一个问题关于 os 模块中的 getcwd

  •  
  •   tin · 2014-07-21 15:17:17 +08:00 · 2809 次点击
    这是一个创建于 3569 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import os
    os.getcwd()就可以看到当前路径,
    但我在看 os.py 这个文件时,没有找到关于 getcwd这个函数请问是为什么?谢谢。
    6 条回复    2014-07-22 02:19:58 +08:00
    timonwong
        1
    timonwong  
       2014-07-21 15:26:10 +08:00
    __all__.extend(_get_exports_list(blahblahblah))

    blahblahblah 是 builtin 的模块, 对应不同平台,比如 windows, posix

    源代码看 Modules/posixmodule.c
    tin
        2
    tin  
    OP
       2014-07-21 15:59:50 +08:00
    谢谢,但没看明白。。。。
    timonwong
        3
    timonwong  
       2014-07-21 16:37:44 +08:00
    @tin

    那就简单的说吧,getcwd这些不是由python实现的,是c实现的
    tin
        4
    tin  
    OP
       2014-07-21 19:35:14 +08:00
    @timonwong 明白了。谢谢。是说 getcwd 不是 os 模块里的函数是吧?而是 builtin 里的?那为什么要用 os.getcwd 调用呢?不好意思。初学者,比较小白。问题可能有点弱。请见谅
    timonwong
        5
    timonwong  
       2014-07-21 19:54:43 +08:00
    @tin
    因为那是平台相关的,Python给这些函数导出了不同的模块名

    sys.builtin_module_names # 所有builtin的模块,你可以试试

    比如在windows上,这个名字是 nt, nt.getcwd
    linux上,这个名字是 posix, posix.getcwd

    把这些导出到 os,至少在接口方面,是平台无关的。
    tin
        6
    tin  
    OP
       2014-07-22 02:19:58 +08:00
    非常感谢,有点明白了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4277 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:13 · PVG 18:13 · LAX 03:13 · JFK 06:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.