shailend
shailend

Reputation: 51

redirecting system call output in python

I wish to execute os.system('ls') in python. the return value of this statement is an error code integer..but I want to get the contents of the present directory as a string. How to accomplish this?

Upvotes: 1

Views: 642

Answers (2)

user2665694
user2665694

Reputation:

Python as build-in functionality like os.listdir() or os.walk() for listing stuff on the filesystem. Running 'ls' yourself is very bad-style. In general look at the documentation of the subprocess module giving you all flexibility for interacting with external commands.

Upvotes: 0

Katriel
Katriel

Reputation: 123772

os.listdir(".")


In general, if you want to call a function and get the arguments, you should use subprocess.Popen(). But a lot of the basic directory stuff is in the os module so you don't have to do that.

Upvotes: 2

Related Questions