0

Write a python program that uses the popen function to obtain the output found by executing the ls -l command. After that find out the number of directories and the number of files after executing the command.

Amro Psut
  • 1
  • 1
  • Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – Tetsujin no Oni Apr 13 '20 at 19:25

1 Answers1

0

You could use os.listdir()

import os
dirs = os.listdir()
print(len(dirs))

if you want to filter out all the directories, you could use this

for x in dirs:
    if not os.path.isdir():
        dirs.remove(x)

Of course, popen could work as well, but I think it is way easier to do it like this.

To change the current directory you can use

os.chdir()

and to display the current directory use

os.getcwd()
Stefan
  • 13
  • 4