user2072826
user2072826

Reputation: 536

Run a specific batch command in python

What if I want to include a single batch command that isn't already in a file in python?

for instance:

REN *.TXT *.BAT

Could I put that in a python file somehow?

Upvotes: 5

Views: 40105

Answers (4)

AjV Jsy
AjV Jsy

Reputation: 6075

I created a test.py containing this, and it worked....

from subprocess import Popen    # now we can reference Popen
process = Popen(['cmd.exe','/c ren *.txt *.tx2'])

Upvotes: 1

user2006656
user2006656

Reputation:

A example use subprocess for execute a command of Linux from Python:

mime = subprocess.Popen("/usr/bin/file -i " + sys.argv[1], shell=True, stdout=subprocess.PIPE).communicate()[0]

Upvotes: 1

Sylvain Leroux
Sylvain Leroux

Reputation: 52000

The "old school" answer was to use os.system. I'm not familiar with Windows but something like that would do the trick:

import os
os.system('ren *.txt *.bat')

Or (maybe)

import os
os.system('cmd /c ren *.txt *.bat')

But now, as noticed by Ashwini Chaudhary, the "recommended" replacement for os.system is subprocess.call

If REN is a Windows shell internal command:

import subprocess
subprocess.call('ren *.txt *.bat', shell=True)

If it is an external command:

import subprocess
subprocess.call('ren *.txt *.bat')

Upvotes: 13

Endoro
Endoro

Reputation: 37569

try this:

cmd /c ren *.txt *.bat

or

cmd /c "ren *.txt *.bat"

Upvotes: 1

Related Questions