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
JCZ2MkKb5S8ZX9pq
V2EX  ›  Python

怎么把 cmd 运行目录传入 Python

  •  
  •   JCZ2MkKb5S8ZX9pq · 2019-12-24 22:36:15 +08:00 · 2683 次点击
    这是一个创建于 1600 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • python 文件位于 c:\python\test.py
    • cmd 运行路径是 c:\run\
    • 运行 c:\run> python c:\python\test.py
    • sys.argv 得到的是 c:\python\test.py
    • 在 python 内怎么获取到 c:\run ?
    7 条回复    2019-12-25 09:45:50 +08:00
    gwy15
        1
    gwy15  
       2019-12-24 22:36:57 +08:00   ❤️ 1
    from pathlib import Path
    Path('.').absolute()
    JCZ2MkKb5S8ZX9pq
        2
    JCZ2MkKb5S8ZX9pq  
    OP
       2019-12-24 22:40:21 +08:00
    @gwy15 感谢,有效,第一次看到这个 module。
    xiaoming1992
        3
    xiaoming1992  
       2019-12-24 22:42:06 +08:00
    我是直接在 python 里执行 os.system("cd ...... & .......")
    lxk11153
        4
    lxk11153  
       2019-12-24 22:42:29 +08:00
    环境变量?
    ysc3839
        5
    ysc3839  
       2019-12-24 23:13:44 +08:00 via Android   ❤️ 2
    这是当前工作目录,可以用 os.getcwd()
    JCZ2MkKb5S8ZX9pq
        6
    JCZ2MkKb5S8ZX9pq  
    OP
       2019-12-24 23:19:56 +08:00
    @ysc3839 对哦。这个更加简洁一点。
    wuwukai007
        7
    wuwukai007  
       2019-12-25 09:45:50 +08:00
    os.child(xxx)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3132 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:38 · PVG 21:38 · LAX 06:38 · JFK 09:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.