2

I am trying to run a simple command in python:

from subprocess import *    
check_output("ls")

When I run this it raises

Error:
WindowsError: [Error 2] The system cannot find the file specified
1
  • Give the full path to the check_output and don't rely on the default Commented Aug 18, 2014 at 1:52

1 Answer 1

3

ls doesn’t exist on Windows; dir does. Furthermore, you may need to pass shell=True, since it’s built in to cmd.exe.

If it’s not a test and you just want to get the contents of a directory, use os.listdir instead.

Sign up to request clarification or add additional context in comments.

6 Comments

hi, but how come the example showed in web always use "ls" as example? that mean I need to run the python in UNIX? why need to put the shell=True?
@user37970: They probably used it because if it is a UNIX system, it’s going to be there. I’m not sure what else they’d have used that would be a whole lot more platform-independent except perhaps echo, but that’s even more likely to be a shell built-in. It needs shell=True because there’s no dir.exe (as far as I know); the functionality is built into cmd.exe, the shell.
get it!!thanks,u clear all my cloud,um,have another question,do you know how to link html files with python file? or python file with perl files?
@user37970: I’m not entirely sure what you’re asking. If you want to make some Python code that generates HTML web-accessible, you could use a CGI script. If you want to call a Perl script from Python, you could use the subprocess module as you were testing here. If you can express your problem more clearly, you can ask another question.
@ocktoofay, sorry for the vague statement,I am newbie to python, let say, I have a perl scipt on UNIX: trial.pl print ("hello" world"); and python is on window: Python SCipt: subprocess.call("perl trial.pl",shell=True) it print "1"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.