how to get output.

I

indu_shreenath

Hey,
I want to get the output of "DIR /AD /B" command to a varriable using
python. How can I do this?
Thanks,
Indu
 
D

Diez B. Roggisch

indu_shreenath said:
Hey,
I want to get the output of "DIR /AD /B" command to a varriable using
python. How can I do this?

Using the subprocess-module.


However, I'm not sure what DIR /AD /B does - but there are many
functions in module os that might deliver what you want without invoking
an external command.

Diez
 
I

indu_shreenath

Hey,

I did write the following:
but it does not work.

import subprocess as sp
try:
= sp.Popen("DIR . /AD /B", stdout=sp.PIPE)
result = p.communicate()[0]
print result
except:
print "error"

This throws error.
DIR . /AD /B will list out only directories in the current directory.

Thanks,
Indu
 
I

indu_shreenath

I corrected a typ below.

Hey,

I did write the following:
but it does not work.

import subprocess as sp
try:
p = sp.Popen("DIR . /AD /B", stdout=sp.PIPE)
result = p.communicate()[0]
print result
except:
print "error"

This throws error.
DIR . /AD /B will list out only directories in the current directory.

Thanks,
Indu

indu_shreenath schrieb:
Using the subprocess-module.
However, I'm not sure what DIR /AD /B does - but there are many
functions in module os that might deliver what you want without invoking
an external command.
Diez- Hide quoted text -

- Show quoted text -
 
P

Peter Otten

I corrected a typ below.

Hey,

I did write the following:
but it does not work.

import subprocess as sp
try:
p = sp.Popen("DIR . /AD /B", stdout=sp.PIPE)
result = p.communicate()[0]
print result
except:
print "error"

This throws error.
DIR . /AD /B will list out only directories in the current directory.

try:
...
except:
print "error"

is a really bad idea because it hides any meaningful and therefore valuable
information about the error.

IIRC the "dir" command is internal to the dos shell and therefore has to be
called indirectly. The actual command is "cmd", not "dir":

[following snippet found at
http://www.python-forum.de/viewtopic.php?p=73048&sid=13808229538bd52de4ef75c32cf67856]
import subprocess
args = ["cmd", "/C", "dir", "J:\\", "/O", "/AD", "/B"]
process = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
print process.stdout.read()

If you just want a list of subdirectories, here's a better approach:
def directories(folder): .... return os.walk(folder).next()[1]
directories(".")
[snip list of subdirectories of cwd]

Peter
 
L

Larry Bates

I corrected a typ below.

Hey,

I did write the following:
but it does not work.

import subprocess as sp
try:
p = sp.Popen("DIR . /AD /B", stdout=sp.PIPE)
result = p.communicate()[0]
print result
except:
print "error"

This throws error.
DIR . /AD /B will list out only directories in the current directory.

Thanks,
Indu

indu_shreenath schrieb:
Hey,
I want to get the output of "DIR /AD /B" command to a varriable using
python. How can I do this?
Using the subprocess-module.
However, I'm not sure what DIR /AD /B does - but there are many
functions in module os that might deliver what you want without invoking
an external command.
Diez- Hide quoted text -
- Show quoted text -
That is better done in python with:

import os
dirs=[d for d in os.listdir(os.curdir) if os.path.isdir(d)]

or

dirs=filter(os.path.isdir, os.listdir(os.curdir))

-Larry
 
I

indu_shreenath

I corrected a typ below.
Hey,
I did write the following:
but it does not work.
import subprocess as sp
try:
p = sp.Popen("DIR . /AD /B", stdout=sp.PIPE)
result = p.communicate()[0]
print result
except:
print "error"
This throws error.
DIR . /AD /B will list out only directories in the current directory.
Thanks,
Indu
indu_shreenath schrieb:
Hey,
I want to get the output of "DIR /AD /B" command to a varriable using
python. How can I do this?
Using the subprocess-module.
However, I'm not sure what DIR /AD /B does - but there are many
functions in module os that might deliver what you want without invoking
an external command.
Diez- Hide quoted text -
- Show quoted text -

That is better done in python with:

import os
dirs=[d for d in os.listdir(os.curdir) if os.path.isdir(d)]

or

dirs=filter(os.path.isdir, os.listdir(os.curdir))

-Larry- Hide quoted text -

- Show quoted text -

Thanks I could implement it.

Indu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top