在不同文件夹中执行命令行 [英] Command line execution in different folder

查看:88
本文介绍了在不同文件夹中执行命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 os.system(command)调用python中的命令行程序。



如何调用此命令传递另一个文件夹以执行该命令?有系统调用吗?或者,我应该保存当前文件夹,并在执行后更改以将其还原。

解决方案

子过程 模块是一个非常好的解决方案。

  import子进程
p = subprocess.Popen([command,arguments1,...],cwd = working_directory)
p.wait()

它还具有修改环境变量,将输入/输出重定向到调用程序等的参数。 / p>

I'm calling a command line program in python using the os.system(command) call.

How can I call this command passing a different folder for execution? There is a system call for this? Or I should save the current folder, and, after execution, change restore it.

解决方案

The subprocess module is a very good solution.

import subprocess
p = subprocess.Popen([command, argument1,...], cwd=working_directory)
p.wait()

It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.

这篇关于在不同文件夹中执行命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆