[英]how can i output a Linux command in python popen
Write a python program that uses the popen function to obtain the output found by executing the ls -l command.编写一个python程序,使用popen function获取执行ls -l命令找到的output。 After that find out the number of directories and the number of files after executing the command.之后找出执行命令后的目录数和文件数。
You could use os.listdir()你可以使用 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.当然,popen 也可以,但我认为这样做更容易。
To change the current directory you can use要更改当前目录,您可以使用
os.chdir()
and to display the current directory use并显示当前目录使用
os.getcwd()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.