Reputation: 51
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
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
Reputation: 123772
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