Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python execute windows cmd functions

I know you can run Linux terminal commands through Python scripts using subprocess

subprocess.call(['ls', '-l'])    # for linux

But I can't find a way to do the same thing on windows

subprocess.call(['dir'])         # for windows

is it possible using Python without heavy tinkering?

Should I stick to good old fashioned batch files?

like image 447
TheLovelySausage Avatar asked Jul 28 '26 19:07

TheLovelySausage


2 Answers

dir is not a file, it is an internal command, so the shell keyword must be set to True.

subprocess.call(["dir"], shell=True)
like image 199
neqtor Avatar answered Jul 30 '26 10:07

neqtor


Try this

import os

os.system("windows command")

ex: for date

os.system("date")
like image 36
fahad Avatar answered Jul 30 '26 08:07

fahad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!