views:

111

answers:

2

Possible Duplicate:
Equivalent of Backticks in Python

When I want to write directly to the command prompt in Perl, I can do something like this:

Perl File test.pl:

$directory = `dir`;  
print $directory;

Which would output something like..

C:\Documents and Settings\joslim\Desktop>perl test.pl

Volume in drive C has no label. Volume Serial Number is EC37-EB31

Directory of C:\Documents and Settings\joslim\Desktop
(and a listing of all the files..)

Can I do this in Python? I've searched around but have had no luck.

Also, can you tell me what this is called? I'm sure there's a more technical term than "writing directly to the command prompt"...

+2  A: 

The equivalent is commands.getoutput. So for your example:

import commands
directory = commands.getoutput("dir")
print directory
Daniel DiPaolo
Ah thank you. I see it's been deprecated and I'm to use the subprocess module now.
joslinm
+2  A: 

What you are referring to in Perl is the backtick operator, which also behaves identically in PHP.

What you are looking to achieve is to execute a command line operation.

The equivalent in Python of the backtick operator, and how to run a command line program and retrieve the output, has been answered in: Equivalent of Backticks in Python

Jon Cram