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

python 调用 cmd 命令问题

  •  
  •   fangazio · 2015-12-11 12:57:07 +08:00 · 2677 次点击
    这是一个创建于 3051 天前的主题,其中的信息可能已经有所发展或是发生改变。

    实现远程关闭计算机的功能
    直接在电脑上输入指令 shutdown /m \192.168.1.211 /s /t 1 没问题
    然后用 python 调用就无法实现功能
    import os
    os.system('shutdown /m \192.168.1.211 /s /t 1')
    运行无法实现功能
    求解

    6 条回复    2015-12-11 20:31:35 +08:00
    kmahyyg
        1
    kmahyyg  
       2015-12-11 13:37:33 +08:00
    考虑把单引号换成双引号试试
    jpyl0423
        2
    jpyl0423  
       2015-12-11 13:53:40 +08:00
    \ 需要转义
    chinuno
        3
    chinuno  
       2015-12-11 14:34:39 +08:00
    建议调用系统命令特别是 Windows 的还是字符串前面加 r 吧。一个一个处理转义麻烦费时间还容易出错
    fangazio
        4
    fangazio  
    OP
       2015-12-11 16:40:27 +08:00
    @chinuno 你的是对的,加 r 谢谢
    skylancer
        5
    skylancer  
       2015-12-11 17:00:27 +08:00
    为什么要用 /作为参数的用法而不用-呢
    shutdown -s 和 shutdown /s 也是等价的啊
    ifaii
        6
    ifaii  
       2015-12-11 20:31:35 +08:00
    把 '\' 换成 '/' 或者前面加 r
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3197 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:35 · PVG 08:35 · LAX 17:35 · JFK 20:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.