Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the dir/s command in Python?

Background

I use the command dir/s in batch files all the time. But, I am unable to call this using python. NOTE: I am using Python 2.7.3.

Code

import subprocess
subprocess.call(["dir/s"])

Error Message

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    subprocess.call(["dir/s"])
  File "C:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

I have tried changing the quotations but nothing has worked.

How would I call the dir/s module using subprocess?

like image 220
xxmbabanexx Avatar asked Mar 04 '13 17:03

xxmbabanexx


2 Answers

How about

subprocess.call("dir/s", shell=True)

Not verified.

like image 139
zzk Avatar answered Sep 20 '22 20:09

zzk


This is a lot different than what you're asking but it solves the same problem. Additionally, it solves it in a pythonic, multiplatform way:

import fnmatch
import os

def recglob(directory, ext):
    l = []
    for root, dirnames, filenames in os.walk(directory):
        for filename in fnmatch.filter(filenames, ext):
            l.append(os.path.join(root, filename))
    return l
like image 32
carlosdc Avatar answered Sep 21 '22 20:09

carlosdc