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.
            Asked
            
        
        
            Active
            
        
            Viewed 20 times
        
    1 Answers
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
