简体   繁体   English

使用 python 中的子进程模块来存储“pwd”命令的结果

[英]Using subprocess module in python to store the result of “pwd” command

I am creating one program.我正在创建一个程序。 In that program I want to have few commands to run automatically using subprocess module.在该程序中,我希望使用 subprocess 模块自动运行一些命令。

Here is my code:这是我的代码:

import subprocess
result = subprocess.run("pwd")

I want to store the location where is the user is now.我想存储用户现在所在的位置。 Like if the user in /home/user/Documents then I want to store that location to result variable.就像/home/user/Documents中的用户一样,我想将该位置存储到结果变量中。

I tried to use result.stdout and result.stderr but both printed None .我尝试使用result.stdoutresult.stderr但都打印了None Even I tried to print result but that do not worked.即使我尝试打印结果,但没有奏效。 Is there any way to get the location where is the user is right now.有什么方法可以获取用户现在所在的位置。 Please reply I need your help.请回复我需要你的帮助。

In the subprocess module you can use: subprocess.check_output() to check output too but there is another alternative too you can use os.getcwd() where the user is in now.在 subprocess 模块中,您可以使用: subprocess.check_output subprocess.check_output()来检查 output 但还有另一种选择,您也可以在用户现在所在的位置使用os.getcwd() Hope it will help.希望它会有所帮助。

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

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