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?
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
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')
try this:
cmd /c ren *.txt *.bat
or
cmd /c "ren *.txt *.bat"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With