By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

how to get output.

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

Aug 9 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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
Aug 9 '07 #2

P: n/a
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
On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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

Aug 9 '07 #3

P: n/a
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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 -

Aug 9 '07 #4

P: n/a
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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 -

Aug 9 '07 #5

P: n/a
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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 -

Aug 9 '07 #6

P: n/a
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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 -

Aug 9 '07 #7

P: n/a
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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 -

Aug 9 '07 #8

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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...f75c32cf67856]
>>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
Aug 9 '07 #9

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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...f75c32cf67856]
>>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
Aug 9 '07 #10

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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...f75c32cf67856]
>>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
Aug 9 '07 #11

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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...f75c32cf67856]
>>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
Aug 9 '07 #12

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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...f75c32cf67856]
>>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
Aug 9 '07 #13

P: n/a
in************@yahoo.co.in wrote:
I corrected a typ below.

On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
>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

On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>>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
Aug 9 '07 #14

P: n/a
On Aug 10, 12:02 am, Larry Bates <larry.ba...@websafe.comwrote:
indu_shreen...@yahoo.co.in wrote:
I corrected a typ below.
On Aug 9, 12:50 pm, indu_shreen...@yahoo.co.in wrote:
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
On Aug 9, 11:46 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>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

Aug 10 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.