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?
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')
subprocess module for such tasks and this is mentioned in the os.system's docs as well.subprocess.call with shell=True, right? I edit my answer accordingly. Thanks to pointing that out!shell = True can be dangerous and is not recommended generally, use shlex.split : subprocess.call(shlex.split('ren *.txt *.bat'))shell=True could be dangerous when invoking user-provided commands (shell command 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.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. docs.python.org/2/library/…
.txtfiles to.bat?