簡體   English   中英

運行cmd.exe命令以及更多功能

[英]run cmd.exe commands and more things

嗨,我想創建一個Python腳本,該腳本執行帶有回顯的命令行cmd.exe並使用以下命令

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

notepad.exe readdocument.txt

然后在此命令行之后必須關閉命令行,並且必須執行一個普通的python腳本,該腳本如下

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

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

如何正確創建此腳本?

我認為您有一個Windows執行環境,您可能正在尋找的是:

  1. 批處理腳本:

    快速介紹: https//www.tutorialspoint.com/batch_script/index.htm

  2. 或Powershell腳本:

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

  3. 或者,您可以使用Python的子流程模塊執行本機命令:

    使用子進程在Windows上運行Python腳本

您是否要將輸出重定向到stdout 如果是這樣,您可以使用以下方法執行此操作:

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

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

如果成功則返回0,否則返回1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM