简体   繁体   English

我如何在 python popen 中执行 output Linux 命令

[英]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.

相关问题 python如何识别:在linux命令中使用os.popen? - How can python recognize : in linux command using os.popen? 如何确定os.popen是否在同一shell中运行linux命令? - How can I find out if os.popen is running the linux command in same shell? Python-如何使用 Popen 获取 Sudo 命令的 Output - Python- How To Get Output Of Sudo Command Using Popen 我如何将 stream 输入到 python Popen 子进程和 stream Z78E6221F6393D1356681DB93 中? - How can I stream input into a python Popen subprocess and stream output from it? 我如何在Popen中执行包含'&'的bash命令 - How can I execute a bash command containing '&' with Popen Python,使用subprocess.Popen进行linux命令行调用?我得到“[Errno 2]没有这样的文件或目录” - Python, using subprocess.Popen to make linux command line call? I'm getting “[Errno 2] No such file or directory” 我如何使用 python 读取 linux 程序的输出(顶部) - How can i use python to read output of linux program (top) 我如何在 Python 中执行 comm Linux 命令 - How can i execute comm Linux command in Python 如何通过 linux 中的命令将 python package 标记为资源目录? - How can I mark a python package as resource directory by command in linux? 使用Popen在Python中执行Powershell脚本,如何获取Powershell脚本的输出并将其更新到网页? - Use Popen to execute a Powershell script in Python, how can I get the Powershell script's output and update it to web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM