0

Hi I want create a python script that executes a command line cmd.exe with echo off and with this comand below

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

notepad.exe readdocument.txt

and then after this command line must close the command line , and must execute a normal python script , which is this one below

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

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

how can I create this script correctly?

  • 2
    Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Martin Tournoij Nov 28 '16 at 07:24
  • If you have `echo off`, why run `cmd.exe`? What's wrong with just running the programs one after the other from Python? – cdarke Nov 28 '16 at 08:10

2 Answers2

0

I think you have a windows execution environment and what you are possibly looking for is:

  1. A batch script:

    Quick intro: https://www.tutorialspoint.com/batch_script/index.htm

  2. Or Powershell script:

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

  3. Alternatively, you may use Python's subprocess module to execute native commands:

    Using subprocess to run Python script on Windows

Community
  • 1
  • 1
Sharad
  • 9,282
  • 3
  • 19
  • 36
0

Do you want to redirect the output to stdout? if so, you can do this by using:

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

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

It will return 0 if successful else 1.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213