Reputation: 536
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
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
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
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