5

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?

Community
  • 1
  • 1
user2072826
  • 528
  • 1
  • 5
  • 12

4 Answers4

11

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')
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • Prefer `subprocess` module for such tasks and this is mentioned in the `os.system`'s docs as well. – Ashwini Chaudhary Jun 15 '13 at 09:21
  • @AshwiniChaudhary For "advanced" use (like capturing output) you are right. For basic usage, system does a decent job ;) Anyway, I'm not sure how will behave "subprocess" while invoking internal shell commands. You have to invoke `subprocess.call` with `shell=True`, right? I edit my answer accordingly. Thanks to pointing that out! – Sylvain Leroux Jun 15 '13 at 09:25
  • `shell = True` can be dangerous and is not recommended generally, use `shlex.split` : `subprocess.call(shlex.split('ren *.txt *.bat'))` – Ashwini Chaudhary Jun 15 '13 at 09:30
  • @AshwiniChaudhary I agree that `shell=True` could be dangerous when invoking user-provided commands ([shell command injection](http://en.wikipedia.org/wiki/Code_injection#Shell_injection)) -- but I don't think we are in that situation here. The "shell command" seems to be provided by the programmer. And (I'm not sure about that) if 'REN' is actually an _internal_ Windows shell command -- you will need a shell. – Sylvain Leroux Jun 15 '13 at 09:34
  • `shell = True` doesn't means the command has to be a shell command, it is required when you're passing a single string to `subprocess`. http://docs.python.org/2/library/subprocess.html#frequently-used-arguments – Ashwini Chaudhary Jun 15 '13 at 09:49
  • I didn't look into the code, but as far as I understand `shell=True` execute the command in a sub-shell (like the old 'system') -- with all the corresponding risks of "shell injection" if the command originate somehow from user provided data. Without `shell=True` the command is directly invoked -- so without shell interpretation of multiple-commands, meta-characters and so on. – Sylvain Leroux Jun 15 '13 at 09:52
  • FWIW, the first example of the two ways work for me on Windows. – martineau Jun 15 '13 at 12:02
1

try this:

cmd /c ren *.txt *.bat

or

cmd /c "ren *.txt *.bat"
Endoro
  • 37,015
  • 8
  • 50
  • 63
1

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]
1

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'])
AjV Jsy
  • 5,799
  • 4
  • 34
  • 30