【问题标题】:run cmd.exe commands and more things运行 cmd.exe 命令等等
【发布时间】:2017-04-11 20:47:30
【问题描述】:

您好,我想创建一个 python 脚本来执行命令行 cmd.exe 并关闭回显并使用下面的命令

unrar.exe -e -w -b compression.rar c:/tmp

notepad.exe 读取文档.txt

然后在这个命令行之后必须关闭命令行,并且必须执行一个普通的python脚本,就是下面这个

os.rename('readdocument.txt','readdocument-done.txt')
import urllib

testfile = urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz", "file.gz")

如何正确创建此脚本?

【问题讨论】:

标签: python shell download rename


【解决方案1】:

是否要将输出重定向到stdout?如果是这样,您可以使用:

subprocess.call(['commands','here']) 

import subprocess                                               
subprocess.call(['unrar', 'e', 'w', 'b', 'compression.rar', 
                             'c:/tmp'], stdout=subprocess.PIPE)

成功则返回0,否则返回1。

【讨论】:

    【解决方案2】:

    我认为您有一个 Windows 执行环境,您可能正在寻找的是:

    1. 一个批处理脚本:

      简介:https://www.tutorialspoint.com/batch_script/index.htm

    2. 或 Powershell 脚本:

      https://blog.udemy.com/powershell-tutorial/

    3. 或者,您可以使用 Python 的 subprocess 模块来执行本机命令:

      Using subprocess to run Python script on Windows

    【讨论】:

    • 我只想用python
    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2016-01-04
    • 2012-10-13
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多