最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Python如何执行外部命令

    正文概述    2020-08-16   406

    Python如何执行外部命令

    调用shell命令

    #!/usr/bin/python3
    
    import subprocess
    
    # 执行外部命令 'date'
    subprocess.call('date')
    
    # 传递选项和参数给命令
    print("\nToday is ", end="", flush=True)
    subprocess.call(['date', '-u', '+%A'])
    
    # 其他例子
    print("\nSearching for 'hello world'", flush=True)
    subprocess.call(['grep', '-i', 'hello world', 'hello_world.py'])

    这里的import语句用于载入subprocess模块,它是Python标准库的一部分

    subprocess模块中的call函数是一种执行外部命令的方式

    通过传递True给flush参数(默认是False),我们确保这个信息在subprocess.call运行之前输出

    想要给命令传递参数,需要使用字符串列表

    $ ./calling_shell_commands.py
    Tue Jun 21 18:35:33 IST 2016
    
    Today is Tuesday
    
    Searching for 'hello world'
    print("Hello World")

    使用扩展调用shell命令

    #!/usr/bin/python3
    
    import subprocess
    
    # 不使用扩展调用Shell命令
    print("No shell expansion when shell=False", flush=True)
    subprocess.call(['echo', 'Hello $USER'])
    
    # 使用Shell扩展调用Shell命令
    print("\nshell expansion when shell=True", flush=True)
    subprocess.call('echo Hello $USER', shell=True)
    
    # 如果引号是命令的一部分则进行反义
    print("\nSearching for 'hello world'", flush=True)
    subprocess.call('grep -i \'hello world\' hello_world.py', shell=True)

    默认subprocess.call不会扩展shell 通配符,使用 command替换等等

    可以设定shell参数为True进行重写

    注意现在整个命令行都作为一个字符串而不是字符串列表

    命令中含有引号如要转义

    仅在你确定命令会正确执行的情况下使用shell=True,否则会存在安全问题

    $ ./shell_expansion.py
    No shell expansion when shell=False
    Hello $USER
    
    shell expansion when shell=True
    Hello learnbyexample
    
    Searching for 'hello world'
    print("Hello World")

    在特定情况下,我们可以使用单/双引号的组合来避免使用转义符号

    # 像这样使用另一种引号
    subprocess.call('grep -i "hello world" hello_world.py', shell=True)
    
    # 或者这样
    subprocess.call("grep -i 'hello world' hello_world.py", shell=True)

    Shell命令重定向可以正常使用

    # 为了避免call函数字符串太常,我们使用变量先存储命令
    cmd = "grep -h 'test' report.log test_list.txt > grep_test.txt"
    subprocess.call(cmd, shell=True)

    避开使用shell=True

    >>> import subprocess, os
    >>> subprocess.call(['echo', 'Hello', os.environ.get("USER")])
    Hello learnbyexample
    0

    os.environ.get("USER")返回环境变量USER的值

    0退出状态码,意味着成功执行了命令。

    获取命令输出和重定向

    #!/usr/bin/python3
    
    import subprocess
    
    # 输出也包含任何的错误信息
    print("Getting output of 'pwd' command", flush=True)
    curr_working_dir = subprocess.getoutput('pwd')
    print(curr_working_dir)
    
    # 获取命令执行的状态和输出
    # 退出状态码不是“0”意味着有什么事情出错了
    ls_command = 'ls hello_world.py xyz.py'
    print("\nCalling command '{}'".format(ls_command), flush=True)
    (ls_status, ls_output) = subprocess.getstatusoutput(ls_command)
    print("status: {}\noutput: '{}'".format(ls_status, ls_output))
    
    # 抑制出错信息
    # subprocess.call()返回命令的状态码 returns status of command which can be used instead
    print("\nCalling command with error msg suppressed", flush=True)
    ls_status = subprocess.call(ls_command, shell=True, stderr=subprocess.DEVNULL)
    print("status: {}".format(ls_status))

    getstatusoutput()的输出是元组数据类型,更多信息和例子在后续章节

    getstatusoutput()和getoutput()老式的函数

    使用有更多特性和安全选项的新函数

    $ ./shell_command_output_redirections.py
    Getting output of 'pwd' command
    /home/learnbyexample/Python/python_programs
    
    Calling command 'ls hello_world.py xyz.py'
    status: 2
    output: 'ls: cannot access xyz.py: No such file or directory
    hello_world.py'
    
    Calling command with error msg suppressed
    hello_world.py
    status: 2

    起源地下载网 » Python如何执行外部命令

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元