简体   繁体   English

重定向python中的系统调用输出

[英]redirecting system call output in python

I wish to execute os.system('ls') in python. 我希望在python中执行os.system('ls')。 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? 怎么做到这一点?

os.listdir(".")


In general, if you want to call a function and get the arguments, you should use subprocess.Popen() . 通常,如果要调用函数并获取参数,则应使用subprocess.Popen() But a lot of the basic directory stuff is in the os module so you don't have to do that. 但是很多基本目录的东西都在os模块中,所以你不必这样做。

Python as build-in functionality like os.listdir() or os.walk() for listing stuff on the filesystem. Python作为内置函数,如os.listdir()或os.walk(),用于列出文件系统上的内容。 Running 'ls' yourself is very bad-style. 自己跑'ls'非常糟糕。 In general look at the documentation of the subprocess module giving you all flexibility for interacting with external commands. 一般来说,查看子进程模块的文档,为您提供与外部命令交互的所有灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM