elishaolade
elishaolade

Reputation: 75

Subprocess error dealing with "ls" argument in call() function

I am new to python, and I want to know what went wrong with this line of instruction. This line of code was used in the example for subprocess in Python's documentation:

subprocess.call(["ls", "-l"]) 

and it basically returned:

WindowsError: [Error 2] The system cannot find the file specified

Python believes that "ls" is a file rather than an argument. Is there a way this can be fixed?

Upvotes: 0

Views: 140

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799140

Yes. Use ["dir", "/w"] ['ipconfig', '/all'] instead.

Upvotes: 1

Óscar López
Óscar López

Reputation: 236114

Is it necessary to call the ls (or dir) command? in Python, you can list a directory's files using os.listdir(path), which will be much easier to implement!

Regarding your question: you're trying to call the ls command, which is native to Unix-like systems. In Windows, you have to use dir instead, as shown in Ignacio's answer.

Upvotes: 3

Related Questions