3

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?

8
  • 1
    ipconfig is a Windows command. This should work (and it does on my computer). Commented Apr 28, 2015 at 17:24
  • 1
    why cant you do the same on windows? That is a windows command. Also if you want the output use check_output("ipconfig") Commented Apr 28, 2015 at 17:24
  • 1
    You can use shell=True check_output("ipconfig",shell=True) Commented Apr 28, 2015 at 17:29
  • 1
    Some Windows commands are built into the shell, such as dir. You need to run the command process itself: 'cmd.exe /c dir'. Edit: or do as @PadraicCunningham suggests. Commented Apr 28, 2015 at 17:29
  • 1
    If you are just looking to get a list of files, you should be using os.listdir or maybe glob.glob rather than shelling out to do a dir. Commented Apr 28, 2015 at 17:34

4 Answers 4

4

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

subprocess.call(["dir"], shell=True)
Sign up to request clarification or add additional context in comments.

Comments

2

Try this

import os

os.system("windows command")

ex: for date

os.system("date")

Comments

1

Almost everyone's answers are right but it seems I can do what I need using os.popen -- varStr = os.popen('dir /b *.py').read()

1 Comment

The documentation for os.popen() states: Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.
0

First of all, to get a directory listing, you should rather use os.listdir(). If you invoke dir instead, you'll have to parse its output to make any use of it, which is lots of unnecessary work and is error-prone.


Now,

dir is a cmd.exe built-in command, it's not a standalone executable. cmd.exe itself is the executable that implements it.

So, you have two options (use check_output instead of check_call if you need to get the output instead of just printing it):

  • use cmd's /C switch (execute a command and quit):

    subprocess.check_call(['cmd','/c','dir','/s'])
    
  • use shell=True Popen() option (execute command line through the system shell):

    subprocess.check_call('dir /s', shell=True)
    

The first way is the recommended one. That's because:

  • In the 2nd case, cmd, will do any shell transformations that it normally would (e.g. splitting the line into arguments, unquoting, environment variable expansion etc). So, your arguments may suddenly become something else and potentially harmful. In particular, if they happen to contain any spaces and cmd special characters and/or keywords.
  • shell=True uses the "default system shell" (pointed to via COMSPEC environment variable in the case of Windows), so if the user has redefined it, your program will behave unexpectedly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.